MongoDB 3.2 c ++驱动程序,使用$ exists

时间:2016-04-05 13:37:56

标签: c++ mongodb c++11 bson mongo-cxx-driver

bsoncxx::builder::stream::document search_builder;

mongocxx::options::find img_find; // This speeds up the queries

search_builder_images.clear();
search_builder_images <<  "_id" << "abc" << "data" << open_document <<"$exists" << true << close_document ;
for (bsoncxx::document::view doc : cursor_cal) {
    std::cout << bsoncxx::to_json(doc) << std::endl;
}

auto cursor_cal = dbMongo [collectionName] .find(search_builder.view());

这里随机有50-50%的几率,我有时会得到我期望的输出,有时候会出现分段错误错误。

我做错了什么? (我正在尝试创建此search_builder以在mongodb数据库中搜索并获取存在数据的文档?)

1 个答案:

答案 0 :(得分:0)

这有点旧,但我在构建文档时遇到了段错误问题,不确定它是否面向您。我不得不将查询文档构造分成多行,例如:

auto queryDoc = document{};
queryDoc << _id << "abc";
queryDoc << "data" << open_document;
  queryDoc << "$exists" << true;
queryDoc << close_document;
auto query = queryDoc << finalize;

希望这有助于其他人。