3
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());

Here randomly 50-50% chances , I sometimes get the output I expect and sometimes I get segmentation fault error.

What am I doing wrong ? ( I am trying to create this search_builder to search in mongodb database and get documents where data exists ? )

1 Answers1

0

This is a bit old but I was having a segfault issue for the construction of the document, not sure if its what you were facing. I had to break the query document construction into multiple lines, such as :

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

Hope this helps someone else.

Victor.dMdB
  • 999
  • 2
  • 11
  • 29