查询一个文档
如何查询一个如下文档的所有值
{
"name" : "MongoDB",
"type" : "database",
"count" : 1,
"versions": [ "v3.2", "v3.0", "v2.6" ],
"info" : {
"x" : 203,
"y" : 102
}
}
mongocxx::stdx::optional<bsoncxx::document::value> result = coll.find_one(document{} <<" name"<<"MongoDB"<<finalize);
if(result){
bsoncxx::document::view view = (*result).view();
std::string Name = view["name"].get_utf8().value.to_string();
std::string Type = view["type"].get_utf8().value.to_string();
int Count = view["count"].get_int32();
for(int i = 0;view["versions"][i];i++){ //若view["versions"][i]无值,则返回
std::cout<<view["versions"][i].get_utf8().value.to_string();
//v3.2 v3.0 v2.6
}
int X = view["info"]["x"].get_int32();
int Y = view["info"]["x"].get_int32();
}
遍历所有文档
mongocxx::cursor cursor = coll.find(document{} << finalize);
for(auto doc : cursor) { //遍历各个文档
int X = doc["info"]["x"].get_int32();
int Y = doc["info"]["x"].get_int32();
}