繁体   English   中英

使用MongoDB C ++驱动程序在BSON文档中查找

[英]find in BSON documents with MongoDB C++ driver

我的MongoDB测试数据库中有以下文档:

  db.a.find()
       {[ {
            "_id" : ObjectId("5113d680732fb764c44qweq"),
            "Builds" : [
                    {
                        "level" : 1,
                        "rank" : 2
                    },
                    {
                        "level" : 3,
                        "rank" : 4
                    }
                  ],
  "abs" : [
                    {
                        "level" : 3,
                        "status" : 5
                    },
                    {
                        "level" : 3,
                        "status" : 4
                    }
                  ]
    }, {
     "_id" : ObjectId("5113d680732fb764c4464fdf"),
            "Builds" : [
                    {
                        "level" : 3,
                        "rank" : 5
                    },
                    {
                        "level" : 3,
                        "rank" : 4
                    }
                  ],
    "abs" : [
                    {
                        "level" : 3,
                        "status" : 5
                    },
                    {
                        "level" : 3,
                        "status" : 4
                    }
                  ]
        }
    ]}

我需要找到构建级别> = 2和<= 5且Abs状态> = 5就像if(builds.leve> = 2 && builds.level <= 5 && abs.status> = 5 && abs.level> = 2)多个条件,需要取一个值的大小您能帮我吗?

这是给您的样品。 我对mongo cxx的了解不多,所以我不确定语法。

bsoncxx::builder::stream::document filter_builder;
filter_builder << "$or" << "Builds.level" 
    << open_document << "$gte" << 1 << "$lte" << 5 << close_document
    << open_document << "abs.status" << "$gte" << 2 << "$lte" << 5 
        << close_document << close_array;

auto cursor = db["your collection name"].find(filter_builder.view());
for (auto&& doc : cursor) {
    std::cout << bsoncxx::to_json(doc) << std::endl;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM