无法将数据添加到MongoDB Atlas "not authorized to execute command { insert: ... }"



可以使用StringURI连接到MongoDB,但是如果我尝试添加模式,则会收到错误消息

MongoDB Connected ..
{ MongoError: not authorized on admin to execute command { insert: "animals", documents: [[{isEndangered true} {dateOfEntry 2019-10-03 22:33:54.852 +0000 UTC} {_id ObjectIdHex("5d967752e762ed56dc4f1dba")} {name Red Panda} {__v 0}]], ordered: true, writeConcern: { w: "majority" }, lsid: { id: {4 [194 242 186 89 84 47 69 212 143 253 85 42 86 82 70 95]} }, txnNumber: 1.000000, $clusterTime: { clusterTime: 6743708686104920070, signature: { hash: [247 92 77 50 
194 115 41 36 114 64 156 130 20 135 223 112 71 207 137 248], keyId: 6728180188896559104.000000 } }, $db: "admin" }
at Connection.<anonymous> (C:UsersxxxxDocuments4_ProgrammeringmongoDB_Nodenode_modulesmongodblibcoreconnectionpool.js:466:61)
at Connection.emit (events.js:197:13)
at processMessage (C:UsersxxxxDocuments4_ProgrammeringmongoDB_Nodenode_modulesmongodblibcoreconnectionconnection.js:364:10)
at TLSSocket.<anonymous> (C:UsersxxxxDocuments4_ProgrammeringmongoDB_Nodenode_modulesmongodblibcoreconnectionconnection.js:533:15)
at TLSSocket.emit (events.js:197:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:150:17)
ok: 0,
errmsg:
'not authorized on admin to execute command { insert: "animals", documents: [[{isEndangered true} {dateOfEntry 2019-10-03 22:33:54.852 +0000 UTC} {_id ObjectIdHex("5d967752e762ed56dc4f1dba")} {name Red Panda} {__v 0}]], ordered: true, writeConcern: { w: "majority" }, lsid: { id: {4 [194 242 186 89 84 47 69 212 143 253 85 42 86 82 70 95]} }, txnNumber: 1.000000, $clusterTime: { clusterTime: 6743708686104920070, signature: { hash: [247 92 77 50 194 115 41 36 114 64 156 130 20 135 223 112 71 207 137 248], keyId: 6728180188896559104.000000 } }, $db: "admin" }',
code: 8000,
codeName: 'AtlasError',
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: {} }

我的连接字符串:(使用包"配置"没有问题[尝试没有](

{
"mongoURI": "mongodb+srv://xxxx:xxxx@xxxx-xxxx.mongodb.net/admin?retryWrites=true&w=majority"
}

使用猫鼬驱动程序和连接:

mongoose
.connect(db, { useNewUrlParser: true, useCreateIndex: true, useFindAndModify: false, useUnifiedTopology: true})
.then(() => console.log('MongoDB Connected ..'))
.catch(err => console.log(err));

我试过:

  • 在网络访问中将我当前的 IP 和 0.0.0.0/0 列入白名单
  • 授权具有两个角色(作为具有正确用户名和密码的 AtlasAdmin 和读写用户(
  • 使用连接字符串:mongodb+srv://:@xxxx-xxxxx.mongodb.net/admin?retryWrites=true&w=majority
  • 我知道 Atlas 的局限性,但我试图简单地添加这样的表格:
const newAnimal = new Animal({
name: 'Red Panda',
isEndangered: true
});
newAnimal
.save()
.then(item => console.log(item))
.catch(err => console.log(err));

这是新手,我甚至不想在开始之前退出。.对于较小的演示和学习来说,一个免费的 Atlas 帐户似乎是一个良好的开端,除了连接和模式等之外,不必处理更多的事情。

错误是:

MongoError: not authorized on admin to execute command { insert: ....

这意味着您正在尝试插入到admin数据库中,这是一个受限制的特殊数据库。

更改连接字符串以使用其他数据库,例如:

"mongodb+srv://xxxx:xxxx@xxxx-xxxx.mongodb.net/test?retryWrites=true&w=majority"

以便连接到名为test的数据库。您的插入物应该可以工作。

正如 Shawn de Wet 在评论中建议的那样,更新猫鼬对我有用(从 5.0.2 到 5.13.15(。

最新更新