Mongocxx and bsoncxx Image store and retrieve


I have some images size varying from 100KB to 500KB(for example). How to store this image in MongoDB and how to read it back. I didn’t find an example in the mongodb git hub repo.
Many artiicles suggested to store the image as binary in mongodb, however I’m not sure if it works or not. Can you please post a sample code to achieve this.


How about GridFS?

Thanks for your suggestion however I don’t want to use GridFs because I don’t want to store my data in two different formats like one in collection which contains basic information and another collection that have image data and then referring it back to the main collection.

If they are smaller than 16mb this article talks about using bindata to do it in the document. It is using nodejs but maybe it’ll give you some inspiration.

Storing Images in MongoDB Using NodeJS | by KRISHNA KISHORE V | Medium.

Thanks. I will go through it and update this thread.

Unfortunately there is no break through for this problem.
I’m able to store the data in bsoncxx::types::b_binary format and able to retrieve it. However cannot convert this binary data to image format. Generally char* data will be used to create an image. In this case, its of type uint8_t*. Don’t know how to move forward.

I’ve seen several posts on SO about converting the binary file to a vector and then storing that in a binary field in the database, and similar in reverse.
C++ is not my primary language (or a distant second) so cannot test this without a lot more effort, if that’s your primary language then perhaps give that a whirl?