使用 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
                    }
                  ]
        }
    ]}

我需要找到 Builds level >=2 and <= 5 和 abs status >=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;
}