Home >>MongoDB Tutorial >MongoDB GridFS

MongoDB GridFS

MongoDB GridFS

GridFS is the MongoDB specification for large files such as images, audio files, video files, etc to be stored and accessed. It's sort of a file system to store files, but its data is stored in the collections of MongoDB. GridFS has the ability to store files that are much greater than its 16MB document size limit.

GridFS divides a file into chunks and stores a separate document for each chunk of data, each with a maximum size of 255k.

By default, GridFS uses two collections of fs.files and fs.chunks to store the metadata and chunks of the file. The unique id ObjectId field defines each chunk. It serves as a parent document for fs.files. In the fs.chunks log, the files id field links the chunk to its parent.

A sample document of the fs.files collection follows –

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

The document specifies the file name, chunk size, uploaded date, and length.

Following is a sample document of fs.chunks document −

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

Adding Files to GridFS

Using GridFS, we can now store an mp3 file using the put command. We will use the Mongofiles.exe utility in the bin folder of the MongoDB installation folder for this function.

Open the prompt, navigate to mongofiles.exe in the MongoDB installation folder's bin folder, and enter the following code −

>mongofiles.exe -d gridfs put song.mp3

Here the name of the database in which the file will be stored is gridfs. MongoDB can automatically generate a new document on the fly if the database is not present. The name of the uploaded file is song.mp3. To access the document file in the database, you can use query find –

>db.fs.files.find()

The above command returned the following document −

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

We can also see all the chunks present in the fs.chunks set with the following code relevant to the stored file, using the document id returned in the previous query,

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

In my case, 40 documents were returned from the query, meaning that the entire mp3 document was divided into 40 chunks of information.


No Sidebar ads