GridFS in MongoDB

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' )}) 

For me, the query returns 40 documents, meaning that all mp3 documents are divided into 40 chunk.

According to Tutorialspoint

Last lesson: Auto-Increment Sequence in MongoDB

Next article: Capped Collection in MongoDB

3.5 ★ | 4 Vote

May be interested

  • Capped Collection in MongoDBPhoto of Capped Collection in MongoDB
    capped collections are fixed-sized circular collection that follow the insert order to enhance the performance of create, read, and delete operations.
  • Overview of MongoDBPhoto of Overview of MongoDB
    mongodb is a cross-platform database, operating on the concepts of collection and document, it provides high performance, high availability and easy scalability.
  • Advantages of MongoDBPhoto of Advantages of MongoDB
    any relation database has a unique schema design to index the data tables and relationships between those tables. meanwhile in mongodb there is no concept of relationship.
  • Install MongoDBPhoto of Install MongoDB
    instructions for installing mongodb on windows.
  • Data modeling in MongoDBPhoto of Data modeling in MongoDB
    data in mongodb has a flexible schema. documents in the same collection need not have the same set of fields or structures, and common fields in collection documents can keep different data types.
  • Create Database in MongoDBPhoto of Create Database in MongoDB
    the use database_name command in mongodb is used to create the database. this command will create a new database, if it does not exist yet, otherwise, this command will return the existing database.