What is GridFS?

GridFS is a MongoDB specification for storing and retrieving large files, such as images, audio, video, and other binary data. It is an effective way to handle files that exceed the BSON document size limit of 16 MB. In this guide, we'll explore the basics of GridFS and how to use it to store and retrieve large files in MongoDB, complete with sample code and examples.


GridFS Components

GridFS breaks down a large file into smaller chunks and stores them as separate documents. It consists of two MongoDB collections:

  • fs.files: Stores metadata about the file, such as the filename and content type.
  • fs.chunks: Contains the binary data chunks of the file.

Storing Large Files with GridFS

To store a large file using GridFS, you can use the MongoDB drivers. Here's a sample code for storing a file in Node.js using the official `mongodb` driver:


const { MongoClient } = require("mongodb");
const fs = require("fs");
// Connection URL
const url = "mongodb://localhost:27017";
const dbName = "myDatabase";
// Initialize a GridFS stream
const client = new MongoClient(url, { useUnifiedTopology: true });
const filename = "large-file.mp4";
async function main() {
try {
await client.connect();
const db = client.db(dbName);
const bucket = new db.GridFSBucket();
// Open a readable stream for the file
const readableStream = fs.createReadStream(filename);
// Upload the file to GridFS
const uploadStream = bucket.openUploadStream(filename);
readableStream.pipe(uploadStream);

console.log("File uploaded to GridFS.");
} catch (error) {
console.error("Error:", error);
} finally {
await client.close();
}
}
main();

Retrieving Large Files from GridFS

To retrieve a large file stored in GridFS, you can use the MongoDB drivers. Here's a sample code for retrieving a file in Node.js:


const { MongoClient } = require("mongodb");
const fs = require("fs");
// Connection URL
const url = "mongodb://localhost:27017";
const dbName = "myDatabase";
// Initialize a GridFS stream
const client = new MongoClient(url, { useUnifiedTopology: true });
const filename = "large-file.mp4";
async function main() {
try {
await client.connect();
const db = client.db(dbName);
const bucket = new db.GridFSBucket();
// Create a writable stream to save the file
const writableStream = fs.createWriteStream("downloaded-file.mp4");
// Download the file from GridFS
const downloadStream = bucket.openDownloadStreamByName(filename);
downloadStream.pipe(writableStream);
console.log("File downloaded from GridFS.");
} catch (error) {
console.error("Error:", error);
} finally {
await client.close();
}
}
main();

Conclusion

GridFS is a useful feature in MongoDB for storing and retrieving large files. With the ability to break files into smaller chunks and handle them efficiently, GridFS is an excellent choice for applications that deal with large binary data.