Mongodb c++ bson query

Skip to first unread message

Chandan L

Oct 2, 2016, 2:06:55 PM10/2/16
Hi All,

I am trying to build mongo query for expression "_id or name and age" in c++.
Function builds this query are not implemented in multiple functions. Top level function creates bsont_t object and initialised and subsequent function calls uses bson_append_array_begin pair . What is the best way to clean up memory? Just calling destroy at top level shall clean up memory?


Dwight Merriman

Oct 2, 2016, 11:17:59 PM10/2/16
note that since this is partially a query question if you need more help you may get more replies on the mongodb_user google group...

you may have already viewed has info: 

for somehting like this json: 

  $or: [
    { _id : 123 },
    { $and : [ { name:"abc" }, { age:33 } ] }

i believe it would be something like 

bsoncxx::builder::stream::document b;
b << 
  "$or" << open_array << 
    open_document << "_id" << 123 << close_document << 
    open_document << 
      "$and" << open_array << 
        open_document << "name" << "abc" << close_document << 
        open_document << "age" << 33 << close_document <<
      close_document << 
    close_array << 

auto cursor = db["mydb"].find(b.view());

i believe the cleanup is automatic when 'b' above destructs.

if above seems verbose you could make local shorthands within a given function or file scope e.g.: 

auto &open_document_type D = open_document;
auto &open_document_type D_ = close_document;

see also 

You received this message because you are subscribed to the Google Groups "BSON" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit

Reply all
Reply to author
0 new messages