neotam (CC0), Pixabay

Most of the application uses file storage and management as their key elements to prove their data processing. File storage very often gets used of a third party operator called Content Delivery Network (CDN) like Amazon web services, however, this may complicate the management. So, it is rather preferable to get access to all the resources of a single cloud storage location instead of from different ones, because there is always a risk of the chance of failure during the time of retrieval.

It used to be a very difficult process of directly storing the files into a database via a single API request until the introduction of the MongoDB – GridFS.

You need to see how GridFS in MongoDB, make use of the indexing and file storage in very smaller sizes to have a quicker retrieval and ways it makes use of appropriate methods used in achieving the end. Let us first understand what is GridFS?


GridFS is one driver specification that is used in the upload and retrieval of files from MongoDB. It is a well-known driver specification that can store and retrieve files of more than the 16 MB limit as of the BSON document. GridFS compartmentalize a file into several portions or chunks and act towards saving each of the chunks as a separate document, instead of storing the entire file as a single document.

Each portion of the file can be up to 255 KB in size which suggests that the final portion is typically equal to or be less than the 255 KB which seems very interesting, isn’t it?

GridFS is the right technique that can store files in MongoDB which facilitates the schema-less and faster retrieval of the data provided by the document model. Since the files are divided into smaller parts, it becomes easier for you to access the specific location of a file, thus saving the memory-intensive tasks like loading the entire file.

When you read from GridFS, the specific driver re-assembles all the portions of the file as required which means that you can read all of the portions of the file based on the questions like listening to a segment of an audio file or retrieval of a segment of a video clip.

Let us see the details of different indexes and their processing:

GridFS Collections MongoDB GridFS Indexes

GridFS uses indexes on each of the portions and collections of a file to give an efficient result and for meeting our convenience, the driver automatically adheres to the specifications to build the indexes.

The GridFS specification offers a simple GridFS API that describes advanced capabilities of GridFS that the driver may opt to offer in its implementations. In addition to this, the work also defines the meaning and purpose of the field concerning the GridFS data model, unspecified document configuration choice, and disambiguates the nomenclature of GridFS. You are also allowed to make the addition of as many indexes as required to meet the ends of the application.

The Chunks Index

Those drivers following the GridFS specification, automatically check for the existence of the aforesaid index before it performs the reading and writing of the operation. If you want to have more information on the behavior of your GridFS app, you may consult your corresponding driver documentation.

If you find that the index doesn’t exist, you have the option to issue the below operation to create the index using the MongoDB Shell (mongoose) which is a fully Node.js 14. x REPL and JavaScript environment to work with MongoDB deployments. Also, you may use MongoDB Shell for direct testing of actions taken against your database.

The Files Index

The Files index uses an index on the collection of files based on the name of the file and upload date columns which is useful in enabling file retrieval, as shown through the following illustration:

If the index doesn’t already exist then you can directly use the MongoDB shell to build it.


To be able to read and write files efficiently in the MongoDB GridFS, your system must satisfy to have installed the software of MongoDB with MongoDB Compass, VS Code, and Node.js.

Now, that you have met all the requirements start following the below listed simple process:

Step 2: In this transitioned workspace, create new folders titled files to write and file stored that will contain required files to be read and saved into and from the database.

Step 4: Maintain project folder structure with a few images, audios, videos in the stored files to read and utilized for writing and reading.

Step 5: Open MongoDB Compass Connect to the MongoDB Database Create a new database named filesDB and collection

Step 6: To write a file in GridFS, create a javascript file with file name writefile.js and run the code

Step 7: To read a file, create a javascript file with the file name readfile.js and run the code.

  1. When to Use the MongoDB GridFS Storage System

The GridFS in MongoDB storage system is used upon fulfilling the following conditions:

  1. When the file system has a limitation on the number of files which can be stored in the directory.
  2. When only a piece of the saved information has to be assessed, it lets you recall sections of the file without the need to go through the whole document.
  3. At the time of distribution of the files and their metadata through geographic replica sets, it lets the metadata automatically get in sync with and deploy information across several targeted computers.


GridFS is nothing less than a unique gift for all those developers who aspire to store a large-sized file in MongoDB. The GridFS acts as a storage system that allows developers across the globe to be able to store big file sizes and retrieve those chunks of the files as required. As a result of which, GridFS is a unique MongoDB feature that is used for a variety of purposes for applications. The advantage of using this method is that only a portion of the file can be ready without the need to load the entire file into the memory, thus making the GridFS an extremely beneficial tool for modern applications.

Previous articleData Analytics Definition and 3 Reasons To Get On Board
Next articleAre you looking for a Title IX advisor in Florida? Here’s what you need to know