如何在mongocxx(c ++)中抑制字段?

时间:2017-09-26 08:58:34

标签: c++ mongodb nosql mongodb-query mongo-cxx-driver

我想知道mongocxx驱动程序(c ++)中下面的代码是什么?

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

1 个答案:

答案 0 :(得分:1)

我的同事找到了答案,你可以使用以下代码:

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

    }

我希望它很有用。