In this lesson, you will learn how to bypass MongoDB’s document size limitations to store massive binary files using GridFS.
The Document Size Wall
In MongoDB, a single document has a hard limit of 16 megabytes. While this is plenty for text-based data or JSON configurations, it is far too small for media files, high-resolution images, or large data exports. If you try to store a file larger than this limit within a standard collection, your application will crash.
Enter GridFS
GridFS is a specification built on top of MongoDB that allows you to store files that exceed the 16MB document size limit. Instead of forcing your file into one giant document, GridFS automatically breaks the file into smaller chunks (usually 255KB each) and stores them across two collections: fs.chunks (the binary data) and fs.files (the metadata).
When you "upload" a file via GridFS in Node.js, the driver handles all the heavy lifting of splitting the data and reassembling it when you need to read the file back.
Conceptual Implementation
To use this, you use the GridFSBucket API provided by the official MongoDB Node.js driver. Here is how you would conceptually stream a file into the database:
const { MongoClient, GridFSBucket } = require('mongodb');
const fs = require('fs');
async function uploadLargeFile(db, filePath) {
const bucket = new GridFSBucket(db, { bucketName: 'myFiles' });
// Create a read stream from the source file
// and pipe it into the GridFS upload stream
fs.createReadStream(filePath)
.pipe(bucket.openUploadStream('large-video.mp4'));
}
By streaming the data, your Node.js application doesn't need to load the entire file into RAM, which is essential for performance. When you need to retrieve the file, you simply use bucket.openDownloadStream(fileId) to stream the chunks back to your client or disk.
Key Takeaway: GridFS allows you to store files of virtually unlimited size by automatically partitioning binary data into manageable chunks, effectively bypassing the 16MB document size constraint.