GridFS in MongoDB
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
May be interested
- Shard in MongoDBsharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
- Learn about Java Driver in MongoDBin the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
- Install MongoDBinstructions for installing mongodb on windows.
- Map Reduce in MongoDBin mongodb documentation, map-reduce is a data processing system that condenses a large amount of data into useful overall results. mongodb uses mapreduce command for map-reduce operation. in general, map reduce is used to handle large data sets.
- Aggregation in MongoDBaggregation can be understood as aggregation. the aggregation operation handles data records and returns calculated results. the operations group the values from multiple documents together, and can perform multiple operations on the grouped data to return a single result. in sql, count (*) and group by are equivalent to aggregation in mongodb.
- Data modeling in MongoDBdata in mongodb has a flexible schema. documents in the same collection need not have the same set of fields or structures, and common fields in collection documents can keep different data types.
- How to use Aggregation Pipeline in MongoDBif you are using mongodb's mapreduce, it is best to switch to aggregation pipeline for more efficient computation.
- Query analysis in MongoDBanalyzing queries is a very important aspect to assess the effectiveness of database and the effectiveness of the designed index. we will explore the two most frequently used queries, $ explain and $ hint.
- Projection in MongoDBin mongodb, projection's meaning is to select only the necessary data instead of selecting the entire data of a document. if a document has 5 fields and you only need 3 fields, you should only select 3 fields from that document.
- Reference Database in MongoDBas shown in the relationship chapter in mongodb, to deploy a standardized database structure in mongodb, we use the referenced relationship concept, also known as manual references, in which we manipulate to store the id of the documents referenced in another document.