Storing Binary Data with MongoDB and C++
Rate this tutorial
In modern applications, storing and retrieving binary files efficiently is a crucial requirement. MongoDB enables this with binary data type in the BSON which is a binary serialization format used to store documents in MongoDB. A BSON binary value is a byte array and has a subtype (like generic binary subtype, UUID, MD5, etc.) that indicates how to interpret the binary data. See BSON Types — MongoDB Manual for more information.
In this tutorial, we will write a console application in C++, using the MongoDB C++ driver to upload and download binary data.
Note:
- When using this method, remember that the BSON document size limit in MongoDB is 16 MB. If your binary files are larger than this limit, consider using GridFS for more efficient handling of large files. See GridFS example in C++ for reference.
- Developers often weigh the trade-offs and strategies when storing binary data in MongoDB. It's essential to ensure that you have also considered different strategies to optimize your data management approach.
- MongoDB Atlas account with a cluster created.
- IDE (like Microsoft Visual Studio or Microsoft Visual Studio Code) setup with the MongoDB C and C++ Driver installed. Follow the instructions in Getting Started with MongoDB and C++ to install MongoDB C/C++ drivers and set up the dev environment in Visual Studio. Installation instructions for other platforms are available.
- Compiler with C++17 support (for using
std::filesystem
operations). - Your machine’s IP address whitelisted. Note: You can add 0.0.0.0/0 as the IP address, which should allow access from any machine. This setting is not recommended for production use.
As part of the different BSON types, the C++ driver provides the b_binary struct that can be used for storing binary data value in a BSON document. See the API reference.
We start with defining the structure of our BSON document. We have defined three keys:
name
, path
, and data
. These contain the name of the file being uploaded, its full path from the disk, and the actual file data respectively. See a sample document below:In the code, these are defined with a
#define
so that it’s easy to modify them from a single place.Let’s add a helper function,
upload
, which accepts a file path and a MongoDB collection as inputs. Its primary purpose is to upload the file to the specified MongoDB collection by converting the file into a BSON binary value and constructing a BSON document to represent the file's metadata and content. Here are the key steps within the upload
function:- Open the file at the given path and get its size.
- The file's size is determined by moving the file pointer to the end of the file and then retrieving the current position, which corresponds to the file's size.
- The file pointer is then reset to the beginning of the file to read the content later.
- Read File Content into a Buffer: A
std::vector<char>
buffer is created with a size equal to the file's size to hold the file's binary data. - Create the BSON binary value.
- To represent the file content as BSON binary value, the code creates a
bsoncxx::types::b_binary
object. - The b_binary object includes the binary subtype (set to
bsoncxx::binary_sub_type::k_binary
), the file's size, and data.
- Create a BSON document with three fields:
name
,path
, anddata
. - Insert the document into the collection.
Let’s write a similar helper function to perform the download. The code below takes the file name, destination folder, and a MongoDB collection as inputs. This function searches for a file by its name in the specified MongoDB collection, extracts its binary data, and saves it to the specified destination folder.
Here are the key steps within the
download
function:- Create a filter query to find the file.
- Use the query to find the document in the collection.
- Extract and save binary data — the binary data is accessed using
bsoncxx::document::view
and then retrieved from the document usingbinaryDocView[FILE_DATA].get_binary()
. - Create a file in the destination folder and write the binary content into the file.
With the helper functions in place to perform upload and download, let’s write the main function that will drive this application. Here are the key steps within the
main
function:- Connect to MongoDB: Establish a connection to MongoDB by creating a mongocxx::client instance.
- Fetch the database (
fileStorage
) and collection (files
) to store the files. - Upload all files found in the specified uploadFolder: Recursively iterate through the folder using
std::filesystem::recursive_directory_iterator
. For each file found, call the upload function toupload
the file to the MongoDB collection. - Download specific files with known filenames (
fileName1
andfileName2
) by callingdownload
function to retrieve and save the files to thedownloadFolder
. - Similarly, download all files in the collection by calling
find({})
to get a cursor and iterate through each document in the collection, extracting the file name and then callingdownload
function to download and save the file to thedownloadFolder
.Note: In a real-world situation, callingfind({})
should be done with some kind of filtering/pagination to avoid issues with memory consumption and performance.
Make sure to get the connection string (URI), update it to
mongoURIStr
, and set the different path and filenames to the ones on your disk.Before executing this application, add some files (like images or audios) under the
uploadFolder
directory.Execute the application and you’ll observe output like this, signifying that the files are successfully uploaded and downloaded.
You can see the collection in Atlas or MongoDB Compass reflecting the files uploaded via the application.
You will observe the files getting downloaded into the specified
downloadFolder
directory.With this article, we covered storing and retrieving binary data from a MongoDB database, using the MongoDB C++ driver. MongoDB's robust capabilities, combined with the ease of use provided by the C++ driver, offer a powerful solution for handling file storage in C++ applications. We can't wait to see what you build next! Share your creation with the community and let us know how it turned out!