在Windows 10上使用MongoDB服务器3.4.4,以下命令可以正常工作:
db.createUser(
{
user: "Billy",
pwd : "123456",
roles :
[
{ role: "userAdmin", db : "biolomics_index" },
{ role: "dbAdmin", db : "biolomics_index" },
{ role: "readWrite", db : "biolomics_index" }
]
}
使用mongocxx 3.1.1 C ++驱动程序运行相同的命令失败:
db.run_command(document{} << "createUser" << open_document <<
"user" << "Billy" <<
"pwd" << "123456" <<
"roles" << open_array <<
open_document << "role" << "userAdmin" << "db" << "biolomics_index" << close_document <<
open_document << "role" << "dbAdmin" << "db" << "biolomics_index" << close_document <<
open_document << "role" << "readWrite" << "db" << "biolomics_index" << close_document <<
close_array << close_document <<
finalize);
错误:
“createUser”的类型错误。找到的字符串,找到的对象: 通用服务器错误。
我找不到生成正确文档的语法。有什么想法吗?
答案 0 :(得分:1)
根据Marc_s,用户名应设置为user
而不是db.run_command(document{} <<
"createUser" << "Billy" <<
"pwd" << "123456" <<
"roles" << open_array <<
open_document << "role" << "userAdmin" << "db" << "biolomics_index" << close_document <<
open_document << "role" << "dbAdmin" << "db" << "biolomics_index" << close_document <<
open_document << "role" << "readWrite" << "db" << "biolomics_index" << close_document <<
close_array <<
finalize);
字段。将您的代码更改为:
db.runCommand
注意:强>
正如Saghm在评论中所提到的,MongoDB shell助手与&#34; run命令&#34;并不完全相同。驱动程序中的等价物。如果您想在database::run_command
中尝试使用驱动程序所需的相同BSON,则可以使用shell mongo
。
createUser
documentation以获取驱动程序中的可用命令和参数。