1

I want to query array consisting of documents according to "size" element.

{
"number": 0,
"features": [{
    "size": 10,
    "angle": 0.13546741008758545,
    "response": 0,
}, {
    "size": 40,
    "angle": 0.3170666992664337,
    "response": 0,
}]

}

I did as follows

 //Create the query filter
 auto filter = bsoncxx::builder::stream::document{}  << "features.size"<< bsoncxx::builder::stream::open_document 
             <<"$gt" << 0 
             <<"$lte" << 30
            << bsoncxx::builder::stream::close_document << bsoncxx::builder::stream::finalize;


    mongocxx::options::find opts;
    opts.projection(bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("features", 1)));
    auto cursor = db["deneme"].find(filter.view(), opts);

    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }

But it returns all elements of "features". How to get only elements that satisfy the condition.

syvlvester
  • 61
  • 7
  • If your question is about constructing the query, use `mongo` shell to debug it and ask about the query only without referencing driver-specific code. – D. SM Aug 24 '20 at 16:12

0 Answers0