GridFS stands for Grid File System. MongoDB GridFS is a specification for storing and retrieving large files (such as images, videos, audio files, or any binary data) in MongoDB that exceed the BSON document size limit of 16 MB.
How It Works
GridFS uses two collections to manage files:
fs.files– Stores file metadata such as filename, upload date, size, and a unique ID.fs.chunks– Stores the actual file data in chunks, each linked to the parent file via an ObjectId.
Each chunk document in fs.chunks looks like this:
{
"_id": ObjectId("..."),
"files_id": ObjectId("..."), // reference to fs.files._id
"n": 0, // chunk sequence number
"data": BinData(...) // binary data
}
Example Flow
- You upload a large file.
- GridFS splits it into chunks (e.g., 255 KB each).
- Each chunk is stored in
fs.chunks. - Metadata about the file (like name, length, and MD5 hash) goes into
fs.files. - When you download or read the file, GridFS reassembles it in order.
Tools & Drivers
Most MongoDB drivers include built-in support for GridFS.
For example:
- Node.js:
GridFSBucketAPI in the officialmongodbdriver. - Python:
gridfsmodule inpymongo. - Mongo Shell: Some tools like
mongofileslet you work with GridFS directly.
How to Add (Upload) Images to GridFS Manually via Command Line
MongoDB provides a built-in tool called mongofiles for working with GridFS from the terminal.
You can use it to upload, list, download, or delete files in GridFS — no programming needed.
Step-by-Step Example
1. Make sure MongoDB is running
If you’re running MongoDB locally:
mongod
2. Open another terminal window
3. Use the mongofiles tool
Here’s the syntax:
mongofiles --db <database_name> put <filename>
Example — Upload an Image
Suppose you have a file named photo.jpg in your current folder.
Run this command:
mongofiles --db=mydatabase put photo.jpg
Explanation:
--db=mydatabase→ stores file in themydatabasedatabaseput photo.jpg→ uploads the file into GridFS
After Uploading
If you open the MongoDB shell and connect to your database:
mongosh
use mydatabase
Then check the collections:
show collections
You’ll see:
fs.files
fs.chunks
Now, you can inspect what’s inside:
db.fs.files.find().pretty()
You’ll get something like:
{
"_id": ObjectId("654240f57b7d9e47f1fce9b2"),
"length": 523424,
"chunkSize": 261120,
"uploadDate": ISODate("2025-11-01T12:00:00Z"),
"filename": "photo.jpg"
}
And in fs.chunks:
db.fs.chunks.find().limit(2).pretty()
Each document looks like:
{
"_id": ObjectId("654240f57b7d9e47f1fce9b3"),
"files_id": ObjectId("654240f57b7d9e47f1fce9b2"),
"n": 0,
"data": BinData(0,"/9j/4AAQSkZJRgABAQ...")
}
To Download the Image Back
If you want to download the file again from GridFS:
mongofiles --db=mydatabase get photo.jpg
This will create a new file named photo.jpg in your current folder.
To Delete the Image
mongofiles --db=mydatabase delete photo.jpg
| Command | Action |
|---|
mongofiles --db=mydb put file.jpg | Upload (store in GridFS) |
mongofiles --db=mydb list | List all stored files |
mongofiles --db=mydb get file.jpg | Download a file |
mongofiles --db=mydb delete file.jpg | Delete a file |
Full Example Workflow
Step 1 — Start MongoDB
mongod --dbpath "C:\data\db"
Step 2 — Upload a file
mongofiles --db=test put 1.png
Step 3 — List files in GridFS
mongofiles --db=test list
Step 4 — Download the file
mongofiles --db=test get 1.png
Step 5 — Delete it (optional)
mongofiles --db=test delete 1.png