💻 Tech
If you’re loading (i.e., using load
function) a .js file that connects to a database, you might encounter this error: MongoServerError[AuthenticationFailed]: Authentication failed.
. This is even after authenticating successfully with mongosh.
Try to use authSource
in your connection string to specify which database MongoDB will look for user’s credentials. By default, when you spin up a new Docker container, the credentials are stored in the admin
database that’s why in the query below, it’s the authSource
value.
// query.js
const db = connect("mongodb://user:password@localhost:27017/somedb?authSource=admin")