GridFS in MongoDB
GridFS is the MongoDB Specification for storing and collecting large files such as images, audio, video files, etc. It is a type of file system to store files but its data is stored inside MongoDB collections. .
Introducing GridFS
GridFS is the MongoDB Specification for storing and collecting large files such as images, audio, video files, etc. It is a type of file system to store files but its data is stored inside MongoDB collections. . GridFS has the ability to store files even when its size is larger than the size limit of 16 MB of the Document.
GridFS divides a file into chunk and stores each piece of data in a separate document, its maximum size is 255k.
By default, GridFS uses two fs.files and fs.chunks Collection to store file metadata and chunk. Each Chunk is identified by the unique _id ObjectId field. fs.files acts as a parent Document. The files_id field in fs.chunks links Chunk to its parent Document.
Below is the sample Document of fs.files collection:
{ "filename" : "test.txt" , "chunkSize" : NumberInt ( 261120 ), "uploadDate" : ISODate ( "2014-04-13T11:32:33.557Z" ), "md5" : "7b762939321e146569b07f72c62cca4f" , "length" : NumberInt ( 646 ) }
Document determines the file name, chunk size, update date, and length.
Below is the sample Document of fs.chunks document:
{ "files_id" : ObjectId ( "534a75d19f54bfec8a2fe44b" ), "n" : NumberInt ( 0 ), "data" : "Mongo Binary Data" }
Add File to GridFS
Now, we will store an mp3 file by using GridFS with the put command. We will use the mongofiles.exe utility located in the bin directory of the MongoDB installation directory.
Open the command prompt, navigate to mongofiles.exe in the bin directory of the MongoDB installation directory and compose the following code:
> mongofiles . exe - d gridfs put song . mp3
Here, gridfs is the name of the Database in which the file will be stored. If this Database does not exist, MongoDB will automatically create a new Document. song.mp3 is the name of the uploaded file. To view the document's file in Database, you use the find query.
> db . fs . files . find ()
The above command will result:
{ _id : ObjectId ( '534a811bf8b4aa4d33fdf94d' ), filename : "song.mp3" , chunkSize : 261120 , uploadDate : new Date ( 1397391643474 ), md5 : "e4f53379c909f7bed2e9d631e15c1c41" , length : 10401959 }
We can also see all Chunk in the fs.chunks collection related to the file stored with the following code, using the document id returned from the previous query.
> db . fs . chunks . find ({ files_id : ObjectId ( '534a811bf8b4aa4d33fdf94d' )})
For me, the query returns 40 documents, meaning that all mp3 documents are divided into 40 chunk.
According to Tutorialspoint
Last lesson: Auto-Increment Sequence in MongoDB
Next article: Capped Collection in MongoDB
You should read it
- MongoDB queries and tasks you should know
- Instructions on 2 ways to install MongoDB on Raspberry Pi
- Index (Mong) in MongoDB
- Advantages of MongoDB
- Data type in MongoDB
- MongoDB malicious code attacks more than 26,000 victims in a week
- Query Document in MongoDB
- Learn about security features and authentication in MongoDB
- ObjectId in MongoDB
- Text Search in MongoDB
- Shard in MongoDB
- Learn about Java Driver in MongoDB