GridFS Support in Spring Data MongoDB

26.7.2012 | 2 minutes of reading time


MongoDB is a highly scalable, document oriented NoSQL datastore from 10gen. For more information have a look at the MongoDB homepage: http://www.mongodb.org . A short introduction to MongoDB can be found at this blog post .


In MongoDB the size of a single record (i.e. a JSON document) is limited to 16 MB. If you want to store and query bigger binary data than that, you have to use the GridFS API of your MongoDB driver. There is also a mongofiles command line tool in the bin folder of your MongoDB installation. We will use that for our examples. In an empty database, there are no GridFS files:

1C:\mongo\bin>mongofiles list
2connected to:

But the first usage of GridFS leads to two new collections:

2MongoDB shell version: 2.0.5
3connecting to: test
4> show collections

fs.files stores metadata, and fs.chunks holds the binary data itself. So far, these collections are empty:

1> db.fs.chunks.count()
3> db.fs.files.count()

Now we insert a first file by switching back to the command line and typing:

1C:\mongo\bin>mongofiles put mongo.exe
2connected to:
3added file: { _id: ObjectId('501004130b07c6ab3fb01fa3'), filename: "mongo.exe", chunkSize: 262144, 
4uploadDate: new Date(1343226899351), md5: "f5e82e7d4b7ae87a1d6e80dfc7f43468", length: 1885696 }

Back on the mongo shell we can check our fs collections:

2MongoDB shell version: 2.0.5
3connecting to: test
4> db.fs.files.count()
6> db.fs.chunks.count()

So uploading the mongo.exe binary produced one file that was split into 8 chunks. So much for that, for more details check out the help with

1mongofiles -help

GridFS Support in Spring Data MongoDB

The Spring Data MongoDB project supports access to the GridFS API since the milestone release 1.1.0.M1. In general, Spring Data is another abstraction layer on top of the more low level MongoDB Java Driver:

Let’s see how to use the GridFS support. First, we grab the latest milestone release …

2   <groupId>org.springframework.data</groupId>
3   <artifactId>spring-data-mongodb</artifactId>
4   <version>1.1.0.M2</version>

… from Spring’s snapshot repository:

2    <id>spring-snapshot</id>
3    <name>Spring Maven SNAPSHOT Repository</name>
4    <url>http://repo.springsource.org/libs-snapshot</url>

Spring Data MongoDB offers a GridFsTemplate to handle the GridFS operations:

1<!-- Connection to MongoDB server -->
2<mongo:db-factory host="localhost" port="27017" dbname="test" />
3<mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory"/>
5<!-- MongoDB GridFS Template -->
6<bean id="gridTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
7  <constructor-arg ref="mongoDbFactory"/>
8  <constructor-arg ref="converter"/>

With that template we can easily read the existing GridFS file we inserted before on the command line:

1@Autowired GridFsTemplate template;
3 @Test public void shouldListExistingFiles() {
4     List<GridFSDBFile> files = template.find(null);
6     for (GridFSDBFile file: files) {
7         System.out.println(file);
8     }
9 }

Running the above example, you should see an output like this:

1{ "_id" : { "$oid" : "4fe9bda0f2abbef0d127a647"} , "chunkSize" : 262144 , "length" : 2418176 , 
2   "md5" : "19c2a2cc7684ce9d497a59249396ae1d" , "filename" : "mongo.exe" , "contentType" :  null  , 
3  "uploadDate" : { "$date" : "2012-06-26T13:48:16.713Z"} , "aliases" :  null }

In order to access the content of the file, you call GridFSDBFile#getInputStream. You can also store and delete GridFS files quite easily. Have a detailed look at the GridFsTemplate .

The full source code of the above example can be found at github.

share post




More articles in this subject area\n

Discover exciting further topics and let the codecentric world inspire you.


Gemeinsam bessere Projekte umsetzen.

Wir helfen Deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.