Search code examples
c++mongodbopencvmongo-cxx-driver

Mongodb C++ API insert binary (images)


I looked around and couldn't find a working example for mongocxx binary insert.

I want to insert images to the MongoDB through mongocxx. I can insert except binary.

I am seeking a working example for binary (image) insert to MongoDB through mongocxx. I am using OpenCV.


Solution

  • Once you've manage to store your image in a buffer (for instance in std::vector), you need to use

    bsoncxx::types::b_binary img {bsoncxx::binary_sub_type::k_binary, 
                                  uint32_t(raw.size()),
                                  raw.data()};`
    

    Then you can pass the img object via the common document{} << ... syntax.