我搜索了半个互联网,但没有找到任何内容,或者发现 db.eval()
的解决方案已经过时。因此我的问题。
我有一些使用 mongo 驱动程序 3.5.0 的代码:
MongoDatabase db = mongoClient.getDatabase 'mydb'
println db.runCommand( new BasicDBObject( eval:'db.version()' ) )
Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on mydb to execute command { eval: "db.version()", $readPreference: { mode: "secondaryPreferred" }, $db: "mydb" }' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "not authorized on mydb to execute command { eval: \"db.version()\", $readPreference: { mode: \"secondaryPreferred\" }, $db: \"mydb\" }", "code" : 13, "codeName" : "Unauthorized" } at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:115) at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:107)
我可以在 shell 中运行此命令:
mongo "mongodb://admin:admin@somehost:27003/mydb?replicaSet=myset" -eval 'db.version()'
输出
3.6.6
如果我从 shell 内部运行命令:
db.runCommand( { 'eval':'db.version()' } )
我也有
{ "operationTime" : Timestamp(1536931496, 11), "ok" : 0, "errmsg" : "not authorized on mydb to execute command { eval: \"db.version()\", $db: \"mydb\" }", "code" : 13, "codeName" : "Unauthorized" }
我正在连接的用户如下所示:
{
"_id" : "mydb.admin",
"user" : "admin",
"db" : "mydb",
"roles" : [
{
"role" : "readWrite",
"db" : "mydb"
},
{
"role" : "dbAdmin",
"db" : "mydb"
},
{
"role" : "userAdmin",
"db" : "mydb"
},
{
"role" : "dbOwner",
"db" : "mydb"
}
]
}
我错过了什么?
我需要 eval
的特殊权限吗?
最佳答案
根据 mongodb 文档,您需要一个授予 anyAction 的角色上AnyResource .
关于java - 在 MongoDB Java 中运行脚本命令/文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52333056/