MongoDB GridFS explained

Many people are aware of the data type called BLOB in the database system that can store huge amount of data.Similarly in MongoDB we have GridFS to manage large size documents.By default MongoDB has the default value as 16 MB for storing the documents. It can store all data such as images,audio,video etc.

How it works:

MongoDB divides the collections unto chunks.A chunk is nothing but a small piece of data. And then it has two collections names fs.chunks and fs.files.

fs.chunks stores the binary chunks and fs.files stores the file’s metadata.

Example:

Now lets see an example of how can we implement GridFS in the MongoDB. For this i am running my Linux machine on a docker with centos.

Step 1: Importing image into the database using a utility mongofiles via command line.

Step 2: Checking whether the DB grifd is created slong with the predefined collections.

Step 3: Viewing the in fs.chunks collection.The data will be stored in the binary format which is not human readable.

Step 4: View other values other than the data in fs.chunks collection.

Step 5: Viewing the fs.files collection whereit provides that extra additional details about the image such as id,chunk size,upload date,length,md5 and filename etc.

Takeaways:

  • Use GridFS if you are facing any challeges realted to the storage in the file system when document size exceeds.
  • For huge media files it provides good performance since the data is divided into small chunks.
  •  To keep your files and metadata automatically synced and deployed across a number of systems.

Thus the file size limitation and media file addition is possible in MongoDB as well. For any clarifications let me know in the comment section.