Search code examples
c++mongodbc++11serializationmongo-cxx-driver

c++ serialization to Mongodb


I am trying to use mongodb as my second tier database for my vector matrix in my C++11 code.

I have std::vector<matrix<float, 0 , 1 >> object_descriptors; vectors and want to insert each of them to mongodb and read back to std::vector<matrix<float, 0 , 1 >> object_descriptors.

like:

for (auto & element : object_descriptors){

    bsoncxx::document::value document =  bsoncxx::builder::basic::make_document
            (kvp("file_name", files[file_count++]),
                         kvp("objectID" , serialize??(element)  ); //i couldnt figure out the proper way. 
                         kvp("created_at", bsoncxx::types::b_date(std::chrono::system_clock::now()));

    bsoncxx::stdx::optional<mongocxx::result::insert_one> result =
                coll.insert_one(document.view());


    auto v = document.view();

    element.size();
}

Solution

  • There are really three parts to your question.

    The first question is, essentially, how do I serialize a matrix object into BSON? The answer, unsurprisingly, is "it depends". Logically, you need to decide on what information should be encoded, and in what format. Do you want an array of arrays encoding? In what order? Or are the matrices sparse, so some sort of list structure would be better? It is up to you to decide how to encode one of your matrices into JSON/BSON.

    The second question is, given an answer to question 1, how to I invoke the bsoncxx builder library to do the encoding. I think the answer to your question is that you will want to write some helpers and use the ability to append callables to builders. Please see the stream customization example for some insight into how to do this. That particular example uses the currently de-emphasized stream operations, but the same techniques apply to the basic builder that you are using.

    The third question is, how do I reassemble my matrix object from a BSON document returned in a query. This again will depend on the structure of the BSON you selected as the answer to the first question. You will need to traverse the returned BSON object using the iterator API over the view, and incrementally construct your matrix object.

    If this all sounds like a lot of work, that is because it is. Frameworks that automate this process of taking language level objects and serializing them into or deserializing them out of a database are known as OxMs, where X may be 'R' for relational databases, or 'D' for document databases.

    There is currently no officially supported C++ ODM for MongoDB. However, there is an experimental and unmaintained version of a C++14 ODM built above mongo-cxx-driver called mangrove. While I can't recommend actually using it since it is unmaintained, you may find reading it to provide some inspiration on how to build a simple domain specific ODM like layer for the types you care about without needing to hand-roll each serialization or deserialization routine.