2

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();
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Rahibe Meryem
  • 269
  • 3
  • 14
  • There was a secondary question in this post that was added in response to the answer below. This is not a good way to ask related questions here, since it confuses the original question for new readers. If you have a subsequent question, make that in a new post please. You can (and perhaps should) link to your first question and explain how the second question relates to the first one. – halfer Apr 01 '18 at 13:02

1 Answers1

4

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.

acm
  • 12,183
  • 5
  • 39
  • 68
  • Hi thanks for your answer. I prefered to use Json dump to serialize my float matrix as : json11::Json(element).dump()) used from https://github.com/dropbox/json11. – Rahibe Meryem Jan 26 '18 at 09:19
  • @RahibeMeryem - How about accepting this answer, if it lead you to that conclusion? – acm Mar 04 '18 at 13:09
  • I accepted. Sorry for delay. I am new both community and c++ environment. thanks – Rahibe Meryem Mar 05 '18 at 14:01