Mongodb 查找中ObjectID的Mongocxx数组 我试图用MungCxx驱动程序填充C++查询。

Mongodb 查找中ObjectID的Mongocxx数组 我试图用MungCxx驱动程序填充C++查询。,mongodb,mongo-cxx-driver,Mongodb,Mongo Cxx Driver,Javascript中的查询类似于: {unit_id: {$in: [ObjectId('58aee90fefb6f7d46d26de72'), ObjectId('58aee90fefb6f7d46d26de73'] } } 我认为下面的代码可以用来生成数组部分,但它不能编译 #include <cstdint> #include <iostream> #include <vector> #includ

Javascript中的查询类似于:

{unit_id: {$in: [ObjectId('58aee90fefb6f7d46d26de72'),
                ObjectId('58aee90fefb6f7d46d26de73']
          }
}
我认为下面的代码可以用来生成数组部分,但它不能编译

#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <bsoncxx/types.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>

using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;

mongocxx::instance instance {};
mongocxx::client client{mongocxx::uri{}};
mongocxx::database db = client["banff_development"];
mongocxx::collection coll = db["units"];

int main()
{
    mongocxx::cursor cursor = coll.find
 (document{} << "provider_id" << bsoncxx::oid("58aee90fefb6f7d46d26de4a") 
 <<  finalize);
    bsoncxx::builder::stream::document unit_filter_builder;
    for (auto a: cursor)
    {
        unit_filter_builder << a["_id"].get_oid();
    }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用bsoncxx::builder::stream::close_数组;
使用bsoncxx::builder::stream::close\u文档;
使用bsoncxx::builder::stream::document;
使用bsoncxx::builder::stream::finalize;
使用bsoncxx::builder::stream::open_数组;
使用bsoncxx::builder::stream::open_文档;
mongocxx::实例{};
mongocxx::客户端{mongocxx::uri{};
mongocxx::数据库db=client[“banff_development”];
mongocxx::coll=db[“单位”];
int main()
{
mongocxx::cursor cursor=coll.find

(document{}要构建数组,您需要使用数组生成器,而不是文档生成器。数组生成器的声明应该是
bsoncxx::builder::stream::array unit\u filter\u builder
。对于各种流生成器类型,您似乎还缺少一些包含

另一方面,最好避开流生成器,因为在使用它时很容易遇到问题。这是正确使用流生成器的好例子。您可以使用基本生成器,而不是使用流生成器,它有一个更简单的实现,并且如果有错误,会给您更明智的错误消息你犯了一个错误