使用MongoDB C ++驱动程序在BSON文档中查找 [英] find in BSON documents with MongoDB C++ driver

查看:321
本文介绍了使用MongoDB C ++驱动程序在BSON文档中查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following document in my MongoDB test database:

  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且绝对状态> = 5 就像if(builds.leve> = 2&&builds.level< = 5&& abs.status> = 5&&abs.level> = 2) 多种条件 并且需要取值的大小 你能帮我吗?

I need find Builds level >=2 and <= 5 and abs status >=5 it like if(builds.leve >=2 && builds.level <= 5 && abs.status >=5 && abs.level>=2) multiple conditions and need take a size of values May you help me?

推荐答案

以下是适合您的示例.我对mongo cxx的了解不多,所以我不确定语法.

Here is an sample for you. I am not much into mongo cxx so i am not sure about the syntax.

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;
}

这篇关于使用MongoDB C ++驱动程序在BSON文档中查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆