MongoDB: Supplemental – GRIDFS Example in Java


As mentioned earlier it is time to do some real stuff in this blogging series. Therefore it is good that this week’s class had an example for storing blobs in MongoDB using Python. Yes, well, Python, you know, that is the programming language with these very strange indentation rules. Thus I thought to myself: Let’s do something really cool and fancy! But I had no idea what thus I ended up doing this in Java ;-).

The internet is full to the brim with examples and tutorials on how to do some Java programming in MongoDB. But I thought it will not hurt if I add another small example on top. The first thing to recommend is the Java Tutorial on the MongoDB homepage. I found it to be an excellent starting point. Looking for the Java libraries for using MongoDB? The search is over!

Of course there is an API documentation available for the Java library and there is also more information on GRIDFS in the corresponding specification. But why bothering reading through all these documents. Let’s jump to some Java example right away.

package de.mongodb.gridfs.sample;
import java.io.File;
import java.io.IOException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.WriteConcern;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSInputFile;
public class GRIDFSSample {
	public static void main(String[] args) throws IOException {
		// Connect to MongoDB (without authentification for the time being)
		// And get a handle on the collection used to store the metadata
		Mongo mongo = new Mongo("localhost", 27017);
		DB db = mongo.getDB("test");
		DBCollection collection = db.getCollection("downloads_meta");
		// The biiiiig file to be stored to MongoDB
		File file = new File("/Users/thomasjaspers/Downloads/mongodb-osx-x86_64-2-1.2.1.tgz");
		// Store the file to MongoDB using GRIDFS
		GridFS gridfs = new GridFS(db, "downloads");
		GridFSInputFile gfsFile = gridfs.createFile(file);
		// Let's create a new JSON document with some "metadata" information on the download
		BasicDBObject info = new BasicDBObject();
                info.put("name", "MongoDB");
                info.put("fileName", "MongoDB-OSX-2-1.2.1");
                info.put("rawName", "mongodb-osx-x86_64-2-1.2.1.tgz");
                info.put("rawPath", "/Users/thomasjaspers/Downloads/");
                // Let's store our document to MongoDB
		collection.insert(info, WriteConcern.SAFE);

Hopefully the example is pretty straightforward and good to understand on its own. Basically there are two things happening there. The blob is stored to MongoDB using GRIDFS. Afterwards a document is created to MongoDB (to a different collection) using the Java-version of the commands we have seen in the course of this blog series. For the sake of keeping the example short absolutely no error handling is done, but any exception will be simple thrown to the command line.

Let’s take a look what we have in MongoDB after running the Java program. Therefore I display the collections that are available inside my test-db in the Mongo-shell.

> show collections

If you followed this blog series you might recognise some of the collections from earlier examples. Especially the ones referring to Star Trek. Just starting to wonder if MongoDB could handle some megaquad of information. Would be handy storing my EMH to MongoDB. Ok, probably that is a different story. Back to the collections and there we can see a few ones that look familiar comparing them to the Java implementation:

  • downloads.chunksMongoDB has created this one to store smaller chunks of the big files. This way it can stay inside the limits of a maximum size of 16 MB it has for one document.
  • downloads.files – This collection contains the document of the actual file, so that we do not have to fiddle around with individual chunks on our own.
  • downloads_meta – This one has been created automatically when we saved the metadata document inside the Java program.

Checking the files-document a lot of – what I would say is – MongoDB internal attributes can be seen, but also the filename that we have given for this file in the Java program. I am not going to look into individual chunks, but let’s have a count to see how many have been created for this document, which by the way had a size of roughly 174,4 MB.

> db.downloads.files.find().pretty()
	"_id" : ObjectId("509ec4dea0eebce38606d798"),
	"chunkSize" : NumberLong(262144),
	"length" : NumberLong(178421760),
	"md5" : "1522b62e75c973b7b1d780695807047c",
	"filename" : "MongoDB-OSX-2-1.2.1",
	"contentType" : null,
	"uploadDate" : ISODate("2012-11-10T21:19:26.256Z"),
	"aliases" : null
> db.downloads.chunks.count()

And finally a look at the metadata that we have created altogether with storing the file. No surprises here I would say.

> db.downloads_meta.find().pretty()
	"_id" : ObjectId("509ec4e2a0eebce38606da42"),
	"name" : "MongoDB",
	"fileName" : "MongoDB-OSX-2-1.2.1",
	"rawName" : "mongodb-osx-x86_64-2-1.2.1.tgz",
	"rawPath" : "/Users/thomasjaspers/Downloads/"

Ok, this was meant to get a very rough first impression on how it would feel to do some Java programming on top of MongoDB. Feels good :-)! I found it really easy to start with, which is a thing I would not say if someone would start using for example JPA on top of some relational database. Of course more hacking is required for a final judgement. I have already a small project in mind …

The MongoDB class series

Part 1 – MongoDB: First Contact
Part 2 – MongoDB: Second Round
Part 3 – MongoDB: Close Encounters of the Third Kind
Part 4 – MongoDB: I am Number Four
Part 5 – MongoDB: The Fith Element
Part 6 – MongoDB: The Sixth Sense
Part 7 – MongoDB: Tutorial Overview and Ref-Card

Java Supplemental Series

Part 1 – MongoDB: Supplemental – GRIDFS Example in Java
Part 2 – MongoDB: Supplemental – A complete Java Project – Part 1
Part 3 – MongoDB: Supplemental – A complete Java Project – Part 2


  • Thomas Jaspers

    Funny thing, while searching for some MongoDB-related information I accidentally found a blog entry from my colleague Tobias blogging on GRDFS using Spring. Of course that one should be referenced here:


  • 14. November 2012 von Kili Liam

    Here I’m again Thomas!

    Nice one. Just to let you know, thanks for your time! 😉

    Just a question: we can use the “downloads.chunks” collection just like a “normal” MongoDB collection, right? Just questioning cause I’m not seeing a real use for it.


  • Thomas Jaspers

    Hi again :),

    I guess you can use the chunks-collections as any other collection, beside the fact that it is probably not advisible to add or delete documents there.

    To my understanding the use is more MongoDB-internal in the way that it uses those chunks-collections to split binary data to sevaral 16MB-sized documents, simply using already existing MongoDB functionality.

    But I am not (yet) a MongoDB-expert ;-).

    – Thomas

  • 21. January 2013 von Kili Liam

    Hi Thomas!

    In my spare time, I was trying to use your sample, from this article, to upload files to MongoDB and download them using C#. Well, it took me a while but I made it! Working with GridFS using Java is much more nicer than in C#! OMG Probably due to my lack of knowledge, I was trying it in the wrong way! I had to do it using Java and them try to simulate the Java code in C#!

    Just to let you know that I’m still reading your blogs! 😉 Moving to “Part 4 – MongoDB: I am Number Four”!

    Kili Liam


Your email address will not be published. Required fields are marked *