MongoDB GridFS
GridFS is used to store and restore files (such as pictures, audio, videos, etc.) that exceed 16M (BSON file limit).
GridFS is also a way of file storage, but it is stored in a MonoDB collection.
GridFS can better store files larger than 16M.
GridFS will divide large file objects into multiple small chunks (file fragments), generally 256k/piece. Each chunk will be stored in the chunks collection as a MongoDB document.
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 the meta data related to the file (filename, content_type, and user-defined attributes) will be stored in the files collection.
The following is 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) }
The following is a simple fs.chunks collection document:
{ "files_id": ObjectId("534a75d19f54bfec8a2fe44b"), "n": NumberInt(0), "data": "Mongo Binary Data" }
GridFS Add Files
Now we use the put command of GridFS to store mp3 files. Call the mongofiles.exe tool in the bin in the MongoDB installation directory.
Open the command prompt, enter the bin directory of the MongoDB installation directory, find mongofiles.exe, and enter the following code:
>mongofiles.exe -d gridfs put song.mp3
GridFS is the data name of the storage file. If the database does not exist, MongoDB will automatically create it. Song.mp3 is the audio file name.
Use the following command to view the document of the file in the database:
>db.fs.files.find()
After the above command is executed, the following document data will be returned:
{ _id: ObjectId('534a811bf8b4aa4d33fdf94d'), filename: "song.mp3", chunkSize: 261120, uploadDate: new Date(1397391643474), md5: "e4f53379c909f7bed2e9d631e15c1c41", length: 10401959 }
We You can see all the chunks in the fs.chunks collection. Below we get the _id value of the file. We can get the chunk data based on this _id:
>db.fs.chunks.find({files_id:ObjectId('534a811bf8b4aa4d33fdf94d')})
In the above example, query Data for 40 documents was returned, meaning the mp3 files were stored in 40 chunks.