Search code examples
c++mongodbnosqlmongodb-querymongo-cxx-driver

how to Suppress Field in mongocxx (c++)?


I want to know what is equivalent of below code in mongocxx driver (c++)?

db.RadarPointsExl.find(
   { age: { $gt: 25, $lte: 50 },
{name:1 }
)

Solution

  • My colleague found answer you can use below code using :

    using bsoncxx::builder::stream::document;
        mongocxx::options::find opts;
        document condition, options;
        mongocxx::instance instance{};
        mongocxx::client client{ mongocxx::uri{} };
        mongocxx::database db = client["RadarDB"];
        mongocxx::collection collection = db["RadarPointsExl"];
    
        condition << "age" << open_document << "$gt" << 25 << "$lte" << 50 << close_document;
        options << "name" << 1;
        opts.projection(options.view());
        mongocxx::cursor cursor = collection.find(condition.view(), opts);
    
    
        for (auto doc : cursor) {
            std::cout << doc["name"].get_utf8().value << "\n";
    
        }
    

    I hope it is useful .