How do I create a bsoncxx::document::view from a bson_t?

I’m writing c++ code that needs to jive with existing c code.

The structure in question has an array of bson_t entries, and I need to create a bsoncxx::document::view from the bson_t objects (to port them to a bsoncxx structure).

What is the easiest way to do this? Am I overthinking it, that is to say, is bson_t compatible to just append to, for example, a bsoncxx::builder::basic::array?

Do I need to access the data buffer and initialize a bsoncxx::document::view manually, and if so, how do I access the buffer from a bson_t pointer?

Sorry for any lack of clarity, and I assure you I have been googling this for a while.

Hi @lnorth, welcome to MongoDB community.

You can use bson_get_data to get the data held by bson_t and pass it to bsoncxx::document::view along with len.

int main()
    //`bsondata` represents the document { "foo" : "bar" }
    uint8_t bsondata[] = {0x12, 0x00, 0x00, 0x00, 0x02, 0x66, 0x6f, 0x6f, 0x00, 0x04, 0x00, 0x00, 0x00, 0x62, 0x61, 0x72, 0x00, 0x00};
    bson_t *bsontDoc = bson_new_from_data(bsondata, sizeof(bsondata));
    char* str = bson_as_canonical_extended_json(bsontDoc, NULL);
    std::cout<<"--------bson_t Doc:--------"<<std::endl;
    printf("%s\n", str);
    // Create a bsoncxx::document::view from the bson_t
    bsoncxx::document::view bsoncxxDoc = bsoncxx::document::view(bson_get_data(bsontDoc), bsontDoc->len);
    std::cout << "--------bsoncxx::document::view Doc-------- \n" << bsoncxx::to_json(bsoncxxDoc) << std::endl;
    return 0;

See related documentation here - bson_t - libbson 1.25.0

Thanks for the response! Is there any way to do such a conversion without using the bson library, like accessing the actual data without the bson api? I’m not sure how to explain it but I’d much rather not handle bson_t at all, I feel like using both libbson and bsoncxx kind of defeats the point of lifting my module to a later standard.

Few questions:

  • Where is the array of bson_t entries coming from? Could they pass the underlying data instead?
  • Even if you have both libbson and bsoncxx together, wouldn’t it be the case only till you port your data over? In other words, wouldn’t this migration be done at some point in time, where you could just start using bsoncxx only with no need of porting?

I’m connected to a capped mongodb collection that is used as a data queue (or maybe more accurately like a stream…). I am not able to change the incoming data, unfortunately. So far, I am only working on one out of many consuming submodules. Structural change would definitely be the best solution yet I am not in the position to make any decisions with repercussions toward other submodules.

So, just to make sure I understand correctly, is this an accurate representation of your situation? You can make changes only in your code, and only want to use bsoncxx/mongocxx?

Note: I am assuming here that your code can’t directly connect to MongoDB collection. If you can, then you can directly fetch the BSON documents using the C++ driver into a bsoncxx document.

Wow. Yes this is accurate enough, the specifics are that my module IS a consumer but has to adhere to a standard defined somewhere upstream and used by all other consumers (bson_t in a predefined structure).

Is your code making direct connection to the MongoDB collection?
Who is responsible for fetching the BSON docs from MongoDB collection and populating it with bson_t?