测试时无权执行命令

now*_*rld 4 mongodb connectivity javascript

这是我的代码:

mongoose.connect(consts.database, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    sslCA: consts.databaseCert,
});
//...
const user = await db.userModel.findOne({
    username: usernameLowerCase
}).exec();
Run Code Online (Sandbox Code Playgroud)

这是我的数据库连接字符串(匿名):

mongodb://myUser:userPW@SG-staging-111.servers.mongodirector.com:27017,SG-staging-43334.servers.mongodirector.com:27017?replicaSet=RS-staging-0&ssl=true&authSource=stagingDB
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

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

Run Code Online (Sandbox Code Playgroud)

数据库用户信息:

myUser  [{"role":"readWrite","db":"stagingDB"}]
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会收到此错误not authorized on test to execute command,并且在返回字符串中$db: "test"我什至没有名为 的数据库test。我可能做错了什么?我最近刚刚添加了这个新用户myUser,但现在我收到此错误。此错误是否意味着用户无权“测试”命令?或者,这是否意味着我正在尝试(以某种方式)连接名为“test”的数据库?

now*_*rld 5

原来/authDBName我的连接字符串中缺少 a 。应该是这样的:

mongodb://myUser:userPW@SG-staging-111.servers.mongodirector.com:27017,SG-staging-43334.servers.mongodirector.com:27017/stagingDB?replicaSet=RS-staging-0&ssl=true&authSource=stagingDB

Run Code Online (Sandbox Code Playgroud)