GridFS is the MongoDB specification for putting away and recovering vast documents, for example, pictures, sound records, feature records, and so on. It is slightly a document framework to store records however its information is put away inside MongoDB collections. There are some examples provided here to showcase how GridFS achieves the feature.

GridFS has the capacity to store records considerably more noteworthy than its document  size point of confinement of 16mb. GridFS separates a record into pieces and stores each piece of information in a different document , each of most extreme size is  255k.

GridFS naturally utilizes two collections ( fs.files and fs.chunks) to store the record’s metadata and the pieces. Each one piece is identified by its exceptional _id ObjectId field. The separates as a guardian document . The files_id field in the report links the piece to its parent.

Emulating is an example document  of fs.files collection:

"filename": "test.txt",
"chunkSize": NumberInt(261120),
"uploadDate": ISODdate("2014-04-13t11:32:33.557z"),
"md5": "7b762939321e146569b07f72c62cca4f",
"length": NumberInt(646)

The document specifies the record name, piece size, transferred date, and length. Showing below is a specimen document of fs.chunks record:

"files_id": ObjectId("534a75d19f54bfec8a2fe44b"),
"n": NumberInt(0),
"information": "Mongo Binary Data"

Adding Files to Gridfs:

Presently, we will store an mp4 record utilizing GridFS utilizing the put summon. For this we will utilize the mongofiles.exe utility present in the folder  – bin, as a part of the installation folder.

Enter the below command in the command prompt:

mongofiles.exe -Id GridFS put song.mp4

The GridFS is the database name where the file will be saved. If the db is not introduced, MongoDB will naturally make a db  on the fly. Song.mp4 is the name of the record uploaded. To see the document in database, you can utilize find command:


The above order gave back where its due document :

_id: ObjectId('534a811bf8b4aa4d33fdfe4d'),
filename: "song.mp4",
chunksize: 765434,
uploadDate: new Date(8764097532973), md5: "e4f53379c909f7bed2e9d631e15c1c41",
length: 10401959

We can additionally see all the lumps display in fs.chunks collection identified with the put away record with the accompanying code, utilizing the report id returned within the past query:


For my situation, the inquiry gave back 30 records implying that the entire mp4 record was separated in 30 lumps of information.

READ  MongoDB Query examples

MongoDB Capped Collections

Capped collections are altered size round collections that take after the insertion request to help superior for make, read and erase operations. By round, it implies that when the settled size assigned to the collection is depleted, it will begin erasing the most established report in the collection without giving any express orders.

Capped collections confine redesigns to the documents  if the redesign brings about expanded record size. Capped collections are best for putting away log data, store information or whatever other high volume information.

Creating Capped Collection:

To make a capped collection, we utilize the ordinary createCollection charge yet with capped alternative as genuine and specifying the most extreme size of collection in bytes.


Notwithstanding collection size, we can likewise confine the quantity of documents in the collection utilizing the max parameter:


If you need to check whether a collection is capped or not, utilize the accompanying is Capped order:


If there is a current collection which you want to change over to capped, you can do it with the accompanying code:


This code would change over our current collection presents on a capped collection.

Querying Capped Collection:

As a matter of course a find  query on a capped collection will show results in  insertion order. Anyhow if you need the records to be recovered in converse request, utilize the sort order as demonstrated in the accompanying code:


There are few other paramount focuses in regards to capped accumulations worth knowing:

  • We can’t erase reports from a capped accumulation
  • There is no default indexes display in a capped accumulation, not even on _Id field
  • While embedding another record, MongoDB does not need to really search for a spot to oblige new document  on the circle. It can aimlessly embed the new record at the tail of the accumulation. This makes embed operations in capped accumulations quick.
  • Additionally, while perusing records MongoDB has quite recently to return documents in the same request as present on circle. This makes the read operation quick.
READ  MongoDB – Introduction