Site icon Database Tutorials

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:

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.

Exit mobile version