Using GridFS within Scala to store large files in MongoDB

GridFS is a specification for storing large files in MongoDB. Some of the features of GridFS are :

* GridFS tends to handle large numbers of files better than many file systems.
* Query Support is good.
* If you have certain files that change a lot then it makes sense to store them in GridFS so you can modify them in one place and all clients will get the updates.
Find more features..

Here we’ll look at how to store the large files in GridFS. Let us elaborate it via taking an example of storing an image in to GridFS.

1. We need to have a connection with MongoDB and we can create this connection as :

2. Now we need to create a GridFS that’ll store the files , within this mongo connection.

3. Create the java inputstream from the file that we want to store in GridFS.

4. We can now call the createFile method of GridFS to store the file. We can specify the
attributes of that file as well.

Here’s the complete code file :

3 thoughts on “Using GridFS within Scala to store large files in MongoDB

  1. Thank you for this tutorial, but unfortunately it does not cover getting files out of mongodb gridfs using casbah. I haven’t found a tutorial that shows this process. The official documentation tells you to use findOne(filename). That’s fine and you get your Gridfsdbfile. But what i wasn’t able to figure out is, how to get the data from the different chunks. If I access the inputStream of the gridfsdbfile i just get the data from one chunk.
    I’d be really happy if you could add some more information on this topic.
    Thank you in advance
    Thomas

  2. Yes, please continue this excellent example. To show how to manipulate the retrieved file would be excellent.

Leave a Reply

%d bloggers like this: