Easy Tutorial
❮ Mongodb Osx Install Mongodb Window Install ❯

MongoDB GridFS

GridFS is used to store and retrieve files that exceed the 16MB BSON file size limit (such as images, audio, video, etc.).

GridFS is also a method of file storage, but it is stored in MongoDB collections.

GridFS can better store files larger than 16MB.

GridFS divides large file objects into multiple small chunks, typically 256KB each, with each chunk being stored as a document in the chunks collection.

GridFS uses two collections to store a file: fs.files and fs.chunks.

The actual content of each file is stored in chunks (binary data), and metadata related to the file (filename, content_type, and custom attributes) is stored in the files collection.

Here is a simple example of an fs.files collection document:

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

Here is a simple example of an fs.chunks collection document:

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

Adding Files to GridFS

Now, we will use the GridFS put command to store an mp3 file. Invoke the mongofiles.exe tool located in the bin directory of the MongoDB installation.

Open the command prompt, navigate to the bin directory of your MongoDB installation, find mongofiles.exe, and enter the following code:

>mongofiles.exe -d gridfs put song.mp3

-d gridfs specifies the database name where the file will be stored. If the database does not exist, MongoDB will automatically create it. Song.mp3 is the name of the audio file.

Use the following command to view the documents of the files in the database:

>db.fs.files.find()

The above command returns the following document data:

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

We can see all the chunks in the fs.chunks collection. With the file's _id value, we can retrieve the chunk data:

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

In this example, the query returns 40 documents of data, indicating that the mp3 file is stored in 40 chunks.

❮ Mongodb Osx Install Mongodb Window Install ❯