BSON size with C driver

Showing 1-3 of 3 messages
BSON size with C driver sptrakesh 10/3/12 7:44 AM

I am working on a standalone BSON C++ API.  At present I have a couple of issues with my BSON serialiser, so am using the C API to generate the bson.  I do not see any functions to set the size (the first 4 bytes of the document as per spec) of the BSON data, neither could I find anything appropriate in the test suite for the C driver.  bsondump generates the following error trace when I use it to read my test bson output

It may be that my logic is false when generating the BSON.  The basic workflow is as follows:

::bson b;
bson_init_size( &b, document.getSize() );
// iterator over elements in document and based on element type invoke appropriate bson_append_xxx functions
bson_finish( &b );
bson_destroy( &b );

bsondump /tmp/doctest.bson
{ "_id" : ObjectId( "506b931b44c193650000feff" ), "double" : 1, "string" : "string value", undefined, "boolean" : true, "date" : Date( 1349209034000 ), "null" : null, "regex" : /^abc/, "integer" : 1, "long" : 2 }
Wed Oct  3 09:39:22 Assertion: 10334:Invalid BSONObj size: 0 (0x00000000) first element: EOO
0x107aa3dd9 0x107aaa67e 0x107a5a93f 0x107d7ba30 0x107d7d85c 0x107d7b475 0x107d79063 0x107d7ce21 0x7fff8c2667e1 
 0   bsondump                            0x0000000107aa3dd9 _ZN5mongo15printStackTraceERSo + 57
 1   bsondump                            0x0000000107aaa67e _ZN5mongo11msgassertedEiPKc + 350
 2   bsondump                            0x0000000107a5a93f _ZNK5mongo7BSONObj14_assertInvalidEv + 847
 3   bsondump                            0x0000000107d7ba30 _ZN5mongo8BSONTool11processFileERKN5boost11filesystem210basic_pathISsNS2_11path_traitsEEE + 1184
 4   bsondump                            0x0000000107d7d85c _ZN8BSONDump5doRunEv + 1212
 5   bsondump                            0x0000000107d7b475 _ZN5mongo8BSONTool3runEv + 997
 6   bsondump                            0x0000000107d79063 _ZN5mongo4Tool4mainEiPPc + 9427
 7   bsondump                            0x0000000107d7ce21 main + 49
 8   libdyld.dylib                       0x00007fff8c2667e1 start + 0
assertion: 10334 Invalid BSONObj size: 0 (0x00000000) first element: EOO

Any idea what I am missing when using the C API for serialising to BSON?

Re: BSON size with C driver sptrakesh 10/3/12 4:36 PM
The C++ API and test suite source code can be viewed at  The API specifications are available in HTML and PDF.  I have cross-posted this message to the MongoDB group as well in hope that someone would shed a light on the proper use of the C API
Re: BSON size with C driver hergertme 11/3/12 2:36 AM
I've written an implementation[1] of BSON in C as well, which has a bunch of BSON buffers[2] created with python and verified in unit tests in C. Unlike the upstream bson driver, mine doesn't require calling bson_finish() all over the place (which is what updates the bson size in the first 4 bytes). You might find some of the tests[3] useful for your purpose.