Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Gridfs in Mongodb: What You Need to Know

Understand Gridfs in Mongodb with clear explanations, practical examples, and useful tips. This updated guide covers the essential concepts and common mistakes.

Table of Contents

Gridfs in Mongodb is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

Introducing GridFS

GridFS is the MongoDB Specification for storing and collecting large files such as images, audio, video files, etc. It is a type of file system to store files but its data is stored inside MongoDB collections.. GridFS has the ability to store files even when its size is larger than the size limit of 16 MB of the Document.

GridFS divides a file into chunk and stores each piece of data in a separate document, its maximum size is 255k.

By default, GridFS uses two fs.files and fs.chunks Collection to store file metadata and chunk. Each Chunk is identified by the unique _id ObjectId field. fs.files acts as a parent Document. The files_id field in fs.chunks links Chunk to its parent Document.

Below is the sample Document of fs.files collection:

 { "filename" : "test.txt" , "chunkSize" : NumberInt ( 261120 ), "uploadDate" : ISODate ( "2014-04-13T11:32:33.557Z" ), "md5" : "7b762939321e146569b07f72c62cca4f" , "length" : NumberInt ( 646 ) } 

Document determines the file name, chunk size, update date, and length.

Below is the sample Document of fs.chunks document:

 { "files_id" : ObjectId ( "534a75d19f54bfec8a2fe44b" ), "n" : NumberInt ( 0 ), "data" : "Mongo Binary Data" } 

Add File to GridFS

Now, we will store an mp3 file by using GridFS with the put command. We will use the mongofiles.exe utility located in the bin directory of the MongoDB installation directory.

Open the command prompt, navigate to mongofiles.exe in the bin directory of the MongoDB installation directory and compose the following code:

 > mongofiles . exe - d gridfs put song . mp3 

Here, gridfs is the name of the Database in which the file will be stored. If this Database does not exist, MongoDB will automatically create a new Document. song.mp3 is the name of the uploaded file. To view the document's file in Database, you use the find query.

 > db . fs . files . find () 

The above command will result:

 { _id : ObjectId ( '534a811bf8b4aa4d33fdf94d' ), filename : "song.mp3" , chunkSize : 261120 , uploadDate : new Date ( 1397391643474 ), md5 : "e4f53379c909f7bed2e9d631e15c1c41" , length : 10401959 } 

We can also see all Chunk in the fs.chunks collection related to the file stored with the following code, using the document id returned from the previous query.

 > db . fs . chunks . find ({ files_id : ObjectId ( '534a811bf8b4aa4d33fdf94d' )}) 

FAQ

What should you know about introducing GridFS?

GridFS is the MongoDB Specification for storing and collecting large files such as images, audio, video files, etc. It is a type of file system to store files but its data is stored inside MongoDB collections.. GridFS has the ability to store files even when.

What should you know about add File to GridFS?

Now, we will store an mp3 file by using GridFS with the put command. We will use the mongofiles.exe utility located in the bin directory of the MongoDB installation directory.

What is Gridfs in Mongodb?

Introducing GridFS GridFS is the MongoDB Specification for storing and collecting large files such as images, audio, video files, etc.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.