English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MongoDB GridFS

GridFS is a MongoDB specification for storing and retrieving large files, such as images, audio files, video files, etc. It is similar to a file system for storing files, but its data is stored in MongoDB collections. GridFS can store even larger files than its document size limit (16mb) files.

GridFS divides files into multiple blocks and stores each data block in a separate document, with the maximum size of each file being255k.

By default, GridFS uses two collections,fs.filesandfs.chunksStore the metadata and blocks of the stored files. Each block is identified by its unique _id ObjectId field. fs.files as the parent document.files_idThe fields in the fs.chunks document link the blocks to their parent.

The following is a sample document of the fs.files collection-

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

This document specifies the filename, block size, upload date, and length.

The following is a sample document of the fs.chunks document-

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

Add the file to GridFS

Now, we will useputCommand to use GridFS to store mp3Files. For this, we will usemongofiles.exeUtilities in the bin folder of the MongoDB installation folder.

Open the Command Prompt, navigate to the bin folder of the MongoDB installation folder, and then enter the following code-

>mongofiles.exe -d gridfs put song.mp3

HeregridfsThe name of the database where the file will be stored. If the database does not exist, MongoDB will automatically dynamically create a new document. Song.mp3is the name of the uploaded file. To view the document of the file in the database, you can use the find query-

>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 use the following code to view all the blocks related to the stored file in the fs.chunks collection using the document ID returned by the previous query:

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

For me, this query returned40 documents, which means the entire mp3The document is divided into40 data blocks.