0

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
                }
              ]
}, {
 "_id" : ObjectId("5113d680732fb764c4464fdf"),
        "Builds" : [
                {
                    "level" : 3,
                    "rank" : 5
                },
                {
                    "level" : 3,
                    "rank" : 4
                }
              ]
    }
]}

I need find level which => 1 and <= 2 May you help me? It is possible do with mongocxx?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
YaroslvaV
  • 85
  • 11
  • Does this answer your question? [find in BSON documents with MongoDB C++ driver](https://stackoverflow.com/questions/40998609/find-in-bson-documents-with-mongodb-c-driver) – Jason Aller Aug 31 '20 at 02:14

2 Answers2

1
mongocxx::cursor cursor = collection.find(
  document{} << "Builds.level" << open_document <<
    "$gte" << 1 <<
    "$lte" << 2
  << close_document << finalize);

for(auto doc : cursor) {
  std::cout << bsoncxx::to_json(doc) << "\n";
}

The mongo raw query which would fetch is the following :

db.a.find({"Builds.level":{$gte:1}, "Builds.level":{$lte:2}})
satish chennupati
  • 2,602
  • 1
  • 18
  • 27
1

Think of you are finding data that greater then 1 and less then from 2 then you are go with mongodb then it's look like following:

db.getCollection('a').find({$and: [{'Builds.level': {$gte: 1}},{'Builds.level': {$lte: 2}}]})