Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MongoDB GridFS


May 17, 2021 MongoDB


Table of contents


MongoDB GridFS

GridFS is used to store and recover files (e.g. pictures, audio, video, etc.) that exceed the 16M (BSON file limit).

GridFS is also a way to store files, but it is stored in a collection of MonoDBs.

GridFS can better store files larger than 16M.

GridFS splits large file objects into several small chunks, typically 256k/piece, each of which is stored in the chunks collection as a document for MongoDB.

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

The actual contents of each file are present in the chunks (binary data), and the file-related meta data (filename, content_type, and user-defined properties) will be present in the file collection.

Here's a simple fs.files collection document:

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

Here's a simple fs.chunks collection document:

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

GridFS adds files

Now let's use GridFS's put command to store mp3 files. Call the MongoDB installation bin's mongofiles .exe directory.

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

>mongofiles.exe -d gridfs put song.mp3

GridFS is the name of the data that stores the file. I f the database does not exist, MongoDB is created automatically. Song .mp3 is the audio file name.

Use the following commands to view the Chinese the database:

>db.fs.files.find()

After the above command is executed, the following document data is returned:

{
   _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, and we get the _id value of the file, and we can get the chunk data from this _id:

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

In the above example, the query returns data for 40 documents, meaning that the mp3 file is stored in 40 chunks.