Introduction to Grid FS:

MongoDB has a 16 MB limit for normal document size. To store larger files, MongoDB offers Grid FS, which divides files into smaller chunks stored as separate documents. GridFS improves MongoDB’s efficiency for large files. This blog covers GridFS in MongoDB – how it works, why it’s useful, and how to use it to store large files in segments.

MongoDB’s GridFS specification enables storing and retrieving large files beyond MongoDB’s 16MB document size limit. GridFS uses MongoDB collections to store file chunks and metadata, providing a filesystem-like API for storing unstructured binary data of any format larger than 16MB, including documents, audio, images, video, and more.

To store large files, it is not necessary to load the entire file into RAM. Instead, chunks of the file can be streamed to the database. Typically, each chunk is 255 KB or smaller, allowing the file to be partitioned into manageable pieces. The file driver reassembles the chunks as needed when a user queries a range of the file. This streaming approach enables reading just the relevant sections of a file based on the query, rather than loading the full file into memory.

Mongofiles:

Mongofiles is a basic command-line utility for performing CRUD operations on GridFS stores. However, it is meant for convenience and is not the primary API for GridFS. Mongofiles relies only on filenames to retrieve files. For proper usage, you should treat GridFS like a regular filesystem and keep filenames unique. For most applications, it is better to use your language’s GridFS API instead of mongofiles.

Syntax: mongofiles <options> <connection-string> <command> <filename or _id>

Open Window PowerShell and go the location where mongofiles.exe is located and keep the files that needs to be uploaded in the mongodb.

Execute the mongofiles put command to uploaded files as below.

Command:  ./mongofiles put 25Jan-DaywiseLearning-UKG.pdf

Verify if the files have been successfully uploaded to MongoDB by executing the commands provided below.

Command: db.fs.files.find()

Recent Posts

Start typing and press Enter to search