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.
1 2 3 | [root@55adc0576561 /]# mongofiles --port 27018 -d gridfs put "/usr/local/jerwin/image.png" 2019-03-20T09:01:59.958+0000 connected to: localhost:27018 2019-03-20T09:01:59.979+0000 added file: /usr/local/jerwin/image.png |
Step 2: Checking whether the DB grifd is created slong with the predefined collections.
1 2 3 4 5 6 | > use gridfs switched to db gridfs > > show collections fs.chunks fs.files |
Step 3: Viewing the in fs.chunks collection.The data will be stored in the binary format which is not human readable.
1 2 3 4 5 6 7 | > db.fs.chunks.findOne() { "_id" : ObjectId("5c90514daa1bf7009830ae55"), "files_id" : ObjectId("5c90514daa1bf7009830ae54"), "n" : 0, "data" : BinData(0,"iVBORw0KGgoAAAANSUhEUgAAAJAAAACQCAYAAADnRuK4AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAHHhJREFUeNrsnQt8FPW1x8/Ozj6y2c0DJCEPwkMgPFLuRQIYsEhQQFQq4gOrUG2vWgWv+GpVFNTW1paq9VpQK1VB5WNVFFHUXkCx5fribQpICG/IhlcSkk32PfO/M5vZ7OzszOw8d2dx/zCf3exjkp357jnnd/7nf8YEP6xhYja9BmK2H8RACOl6MI0IiZnacOZWy89OUhtBbWHm/g8CsHMBIBPPzxgDCRck+r6F2mzUZmUg0mqEqC1IbQEGJCQAThQwxHkuC1AaLQ3bmmDMzzmsxyPPXXTPy9XhgNdqy+s5gSTCuNlqLzdbbKX06UPUP9zmqI4eFNYRYs4ugujr6P9EyF9PEoQn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saXui9ue73qHu0o29vSCxR0/WhSWHiLaUOOPO8urW1t42bKBzrhkDl6xSjmSvSVNxOx0wr/m85ZFZD9RvYlxWO8hosZIFSDpENiawtj91/9ABc2/q+2ye0zxaWmxjzOL2g8f9SxY8d+Rvb3ddTsDPbAGjw5NpALHdmZkJrmmQnG89O3LSFRN73eHKNY/WZ6WEPqP5bPi9pW81/eWxpUf3M7FOEGJzXgRkQKlipgHEjtmizQ5yqK2Avv/SE8OHXzOleE5hPj6527UZrLg9FEKNx04GVt+/+NDyDz5vicY5Pki8EmBG1HhkIkBskEwMRDbmlrZKtumTevV+8NZ+Uyv7OS4pzMNHmc1KYNI0vmlynwpuXLOx5YN7/3iojgVNtIm3qv48WYC0ASkaI0XdW3TLeefPVRNHDM4d27uXtTrfaa7WO1CmgfF4ib3HTwS3vvXJ6fWLX21sYFmZ6KaoG1gWoNS4NzZQUZAw5jHb2heqaitKbT/Ky8X6FLjwSmcOVtllpeQPr4+s9wYId3NruP7YicCer7/z1C1acnQfxNqwkCxouMCgTD/g5xpAfCBhHCtFuzorB6quHMzyqspehZZukCj31yfHjlXQ9ykX9CX7Fwy/asdW7rFkwcJ2S9xOYOcEOOc6QHwwAcfNYaB9EjUKCl8nMXQuHtwfAkBi1kmXY3ouA8MLEEIIsiM7lI7/F2AA38MER7Gl+pkAAAAASUVORK5CYII=") } |
Step 4: View other values other than the data in fs.chunks collection.
1 2 3 4 5 6 | > db.fs.chunks.findOne({},{"data":0}) { "_id" : ObjectId("5c90514daa1bf7009830ae55"), "files_id" : ObjectId("5c90514daa1bf7009830ae54"), "n" : 0 } |
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.