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 :