I’m using the driver in Nodejs without Mongoose or any such extra layers. In the following procedures, everything comes from the manual, and it looks as though I am specifying the right credentials (user, password, and database), but nevertheless, an error message comes back “MongoError: command insert requires authentication”.
The same server has another database with its own user, and those are working with software that I installed, but didn’t write. The data about the users in the “admin” database looks parallel between the two of them.
Here I specify the username and password in the arguments to the “connect” operation rather than in the connection URI as I have seen in some examples. Does sending them this way simply not work? What else is there to investigate to try to explain why I am getting an error message that mentions authentication?
app.db_uri = "mongodb://127.0.0.1:27017";
typeof (app.MongoClient = require("mongodb").MongoClient);
typeof ( app.mongo_client = new app.MongoClient(
app.db_uri, {useUnifiedTopology: true}
) );
( async function () { with (app) {
try {
await mongo_client.connect({
'auth.user': "archive",
'auth.password': db_pw,
authSource: "archive"
});
console.log("Connected.");
} catch (err) {
console.error(err)
}
}})();
with (app) typeof (self.db = mongo_client.db("archive"));
( async function () { with (app) {
try {
self.tst_coll = await db.collection("tst");
console.log("Have handle to collection.");
} catch (err) {
console.error(err)
}
}})();
( async function () { with (app) {
try {
await tst_coll.bulkWrite(
[
{ insertOne: { document: {
black: 'lives',
matter: ['you', 'bet']
}}}
]
);
console.log("Wrote.");
} catch (err) {
console.error(err)
}
}})();
MongoError: command insert requires authentication.