Search code examples
mongodbbsonmongo-c-drivermongo-cxx-driver

mongo c++ Bsonbuilder, BSONObj to final c BSON


The underlying goal here is read json with arrays sub documents and whatever in string format {key:"value", boolkey, true, {subkey: value}, array: [x,y,z] } and not caring to undertake append_int, append_bool and all that jazz.

any way of using the convenient C++ BSONObj, mongo::fromjson BSONObjBuilder to finally output a C bson_t.

ie. along the lines of either

 mongo::BSONObj& query = mongo::fromjson ( szJSON );
 bson_t bson = query.c_obj();

or

 BSONObj Builder builder;
 ... (build, build, build)

 BSONObj obj = builder.obj();

 bson_t bson = obj.c_obj();

given that c++ is built on the c driver one would think that the bson_t is lying within somewhere


Solution

  • You can use

    bson_t* b = bson_new_from_data(obj.objdata, obj.objsize)
    

    but you will always create objects twice.