Here is my code:

mongoose.connect(consts.database, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    sslCA: consts.databaseCert,
const user = await db.userModel.findOne({
    username: usernameLowerCase

Here is my DB connection string (anonymized):

mongodb://myUser:[email protected]:27017,SG-staging-43334.servers.mongodirector.com:27017?replicaSet=RS-staging-0&ssl=true&authSource=stagingDB

I'm getting this error:

MongoError: not authorized on test to execute command {
    find: "users",
    filter: {
        username: "bob"
    projection: {},
    limit: 1,
    singleBatch: true,
    batchSize: 1,
    returnKey: false,
    showRecordId: false,
    lsid: {
        id: UUID("0a9400e3-83e3-429c-b8c9-92ade2ff210e")
    $clusterTime: {
        clusterTime: Timestamp(1613200171, 1),
        signature: {
            hash: BinData(0, FED473B580D13E7E5073756DB5140981AADB2985),
            keyId: 6928615819992774977
    $db: "test"

DB user's info:

myUser  [{"role":"readWrite","db":"stagingDB"}]

I have no clue why I am getting this error not authorized on test to execute command, and in the return string $db: "test" I don't even have a database named test. What could I be doing wrong? I just recently added this new user myUser, but now I'm getting this error. Does this error mean that the user is not authorized to "test" commands? Or, does it mean that I am trying to (somehow) connect with a DB named "test"?


1 Answer 1


Turns out that I was missing a /authDBName in my connection string. It should be this:

mongodb://myUser:[email protected]:27017,SG-staging-43334.servers.mongodirector.com:27017/stagingDB?replicaSet=RS-staging-0&ssl=true&authSource=stagingDB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.