gpt4 book ai didi

c++ - CreateUser 使用 mongocxx 3.1.1 C++ 失败

转载 作者:行者123 更新时间:2023-11-30 05:11:53 33 4
gpt4 key购买 nike

在 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" had the wrong type. Expected string, found object: generic server error.

我找不到生成正确文档的语法。有什么想法吗?

最佳答案

根据 createUser documentation ,用户名应该作为 createUser 的值而不是 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);

注意:
正如 Saghm 在评论中提到的,MongoDB shell 助手与驱动程序中的“运行命令”等价物并不完全相同。如果您想尝试使用驱动程序在 database::run_command 中所需的相同 BSON,您可以使用 shell 的 db.runCommand

  1. 查询 Database Commands Manual 以获取驱动程序中可用的命令和参数。
  2. mongo Shell Methods Documentation 描述了通过 mongo shell 与 MongoDB 服务器交互的方法、命令和参数。

关于c++ - CreateUser 使用 mongocxx 3.1.1 C++ 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44840145/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com