我想知道mongocxx驱动程序(c++)中的以下代码等效吗?

db.RadarPointsExl.find(
   { age: { $gt: 25, $lte: 50 },
{name: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";

    }

我希望它是有用的。

10-08 19:19