Storing binary data in Mongo

So far, we saw 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 would need to store files in a database. In relational databases, the BLOB data type is most commonly used to address this requirement. MongoDB also 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 as of the writing this book. In this recipe, we will store a small image file into Mongo's document and also retrieve it later. If the content you wish to store in MongoDB collections is greater than 16 MB, then MongoDB offers an out of the box solution called GridFS. We will see how to use GridFS in another recipe later in this chapter.

Getting ready

Look at the recipe Installing single node MongoDB in Chapter 1, Installing and Starting the Server and start a single instance of MongoDB. Also, the program to write binary content to the document is written in Java. Refer to the recipes Executing query and insert operations using a Java client, Implementing aggregation in Mongo using a Java client and Executing MapReduce in Mongo using a Java client in Chapter 3, Programming Language Drivers, for more details on Java 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 Packt site. 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 file pom.xml is found.

How to do it…

  1. On the operating system shell with the pom.xml present in the current directory of the mongo-cookbook-bindata project, execute the following command:
    $ mvn exec:java -Dexec.mainClass=com.packtpub.mongo.cookbook.BinaryDataTest
    
  2. Observe the output; the execution should be successful.
  3. Switch to mongo shell that is connected to the local instance and execute the following query:
    > db.binaryDataTest.findOne()
    
  4. Scroll through the document and take a note of the fields in the document.

How it works…

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 BinData, where we see the data encoded in Base64 format.

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 above, two fields 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 the DBObject as a byte array, it gets stored automatically as the BSON type BinData in the document.

See also

What we saw in this recipe is straightforward as long as the document size is less than 16 MB. If the size of the files stored exceeds this value, we have to resort to solutions like GridFS, which is explained in next recipe Storing large data in Mongo using GridFS.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset