说检索特定字段的值,我插使用蒙戈命令行或shell下列文件:如何使用蒙戈CXX司机
db.Users.insert(
{
"user info":{
"user name" : "Joe",
"password" : "[email protected]#%$%" ,
"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消息给我。
请让我知道你是否有更好的点子。
要包括在查询中,这是行不通的突出部分。提供一个'''''mongocxx :: options :: find'''对象作为mongocxx :: collection :: find'''的第二个参数,通过调用mongocxx :: options :: find :: projection'''方法。 – acm
感谢您的回复,请给我看一些例子或链接? – Joe