比如说,我使用 mongo 命令行或 shell 插入了以下文档:
db.Users.insert(
{
"user info":{
"user name" : "Joe",
"password" : "!@#%$%" ,
"Facebook" : "aaa",
"Google" : "joe z"
}
}
)
然后该条目使用系统创建的 ID 登录到数据库中。
如果我想实现以下命令行,它只返回特定字段的值(在这种情况下为_id),使用 cxx 驱动程序我应该怎么做?
这是命令行:
db.Users.find({"user info.user name": "Joe"}, {"_id":1})
我尝试了以下 C++ 代码
bsoncxx::builder::stream::document document{} ;
document<<"user info.user name"<<"Joe"<<"_id"<<1;
auto cursor = myCollection.find(document.view());
for (auto && doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
它根本没有给我任何东西。
如果我设置
document<<"user info.user name"<<"Joe"
然后它会为我返回整个 JSON 消息。
如果您有更好的想法,请告诉我。