GridFS is used to store and restore files (such as 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 MonoDB.
GridFS can better store files larger than 16m.
GridFS will split the large file object into several small chunk (file fragments), usually 256k/, and each chunk will be stored in the chunks collection as a document (document) of MongoDB.
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, as well as user-defined properties) will be stored in the files collection.
The following is simple
fs.files
Collection documents:
{
"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"
}
3.43.1. GridFS add Files ¶
Now we use GridFS’s put command to store the mp3 file. Call the mongofiles.exe tool for bin under the MongoDB installation directory.
Open a command prompt, go to the bin directory of the MongoDB installation directory, find mongofiles.exe, and enter the following code:
>mongofiles.exe -d gridfs put song.mp3
-d
gridfs
Specifies the name of the database in which the file is stored, and if the database does not exist, MongoDB creates it automatically. If the database does not exist, MongoDB creates it automatically. Song.mp3 is the audio file name.
Use the following command to view the documents for files in the database:
>db.fs.files.find()
The following document data is returned after the above command is executed:
{
_id: ObjectId('534a811bf8b4aa4d33fdf94d'),
filename: "song.mp3",
chunkSize: 261120,
uploadDate: new Date(1397391643474), md5: "e4f53379c909f7bed2e9d631e15c1c41",
length: 10401959
}
We can see
fs.chunks
For all the blocks in the collection, we get the_ id value of the file below, and we can obtain the data of the chunk based on this_ id:
>db.fs.chunks.find({files_id:ObjectId('534a811bf8b4aa4d33fdf94d')})
In the above example, the query returns data from 40 documents, meaning that the mp3 file is stored in 40 blocks.