So far we have seen how to store text values, dates, and numbers fields in a document. Binary content also needs to be stored at times in the database. Consider cases where users upload their photographs or scanned copies of documents that need to be stored in the database. In relational databases, the BLOB data type is the most commonly used type to address these requirements. Mongo too supports binary contents to be stored in a document in the collection. The catch is that the total size of the document shouldn't exceed 16 MB, which is the upper limit of the document size at the time of writing this book. In this recipe, we will store a small image file in Mongo's document and also retrieve it later. If the content you wish to store in MongoDB collections is greater than 16 MB, MongoDB offers an out-of-the-box solution called GridFS. We will see how to use GridFS in the Storing large data in MongoDB using GridFS recipe later in this chapter.
Look at the Single node installation of MongoDB recipe in Chapter 1, Installing and Starting the MongoDB Server, and start a single instance of MongoDB. Also, the program to write binary content to the document is written in Java. For more details on Java drivers, refer to the following recipes in Chapter 3, Programming Language Drivers:
Open a Mongo shell and connect to the local MongoDB instance listening to port 27017
. For this recipe, we will be using the project mongo-cookbook-bindata
. This project is available in the source code bundle downloadable from the book's website. The folder needs to be extracted on the local filesystem. Open a command-line shell and go to the root of the project extracted. It should be the directory where the pom.xml
file is found.
pom.xml
file present in the current directory of the mongo-cookbook-bindata
project, execute the following command:$ mvn clean compile exec:java -Dexec.mainClass=com.packtpub.mongo.cookbook.BinaryDataTest
> db.binaryDataTest.findOne()
If we scroll through the large document printed out, we see that the fields are fileName
, size
, and data
. The first two fields are of type string and number respectively, which we populated on document creation, and hold the name of the file we provide and the size in bytes. The data field is a field of BSON type, where we see the data encoded in the base64
format.
What we did to insert this document is not much from an application's perspective. The following lines of code show how we populated the DBObject that we added to the collection:
DBObject doc = new BasicDBObject("_id", 1); doc.put("fileName", resourceName); doc.put("size", imageBytes.length); doc.put("data", imageBytes);
As we see, two fields, namely, fileName
and size
, are used to store the name of the file and the size of the file, and are of type string and number respectively. The field data is added to DBObject
as a byte array. It gets stored automatically as the BSON type BinData
in the document.
What we saw in this recipe is straightforward, as the document size is less than 16 MB, which is the maximum document size in Mongo as of writing this book. If the size of the files stored exceeds this value, we have to resort to solutions such as GridFS, explained in the next recipe.