Mongodb – Not authorized on test to execute command


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):


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"?

Best Answer

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