Skip to main content

Command Palette

Search for a command to run...

Upload Files in MongoDB GridFs using an express server without any additional library.

Updated
3 min read
Upload Files in MongoDB GridFs using an express server without any additional library.
A
Hey there! I'm Aadarsh Guru, on the lookout for exciting growth opportunities. My passion for technology is matched only by my dedication to continuous learning. You'll often find me buried in books, soaking up knowledge to fuel my projects and discussions. I love sharing what I learn, by mentoring others. Beyond coding, I'm also honing my interpersonal skills and delving into psychology and intellectual pursuits. I truly believe in the power of education to drive human advancement and strive to inspire others to embrace learning. Let's connect and create something amazing together! Not just for progress but for purpose.

Introduction:

In this comprehensive guide, we will delve into the intricacies of uploading and retrieving files in MongoDB GridFS, employing a streamlined approach through your Express server. Unlike the previous article, which showcased the utilization of the 'multer-gridfs-storage' npm package, we will now demonstrate an efficient method devoid of additional npm packages. Accompanying this process, we will also explore the retrieval of files from the GridFS bucket. The article is accompanied by a practical example and detailed steps to ensure a seamless implementation.

Step 1: Uploading Files to MongoDB GridFS

Before commencing the file upload process, ensure your primary Express server is fully configured and integrated with your MongoDB database.

  1. Importing Dependencies: Initiate the process by importing essential dependencies such as Express, Mongoose, and Multer. The latter facilitates data parsing, while Mongoose establishes the connection with the MongoDB database.
import express from "express";
import mongoose from "mongoose";
import multer from "multer";
import { Readable } from 'stream';

const route = express.Router();
const upload = multer();
  1. Creating the Upload API Route: Define a designated API route for uploading files to the GridFS storage.
route.post('/upload', upload.single('file'), async (req, res) => {
    try {
        const gridFsBucket = new mongoose.mongo.GridFSBucket(mongoose.connection.db);
        const uploadFile = gridFsBucket.openUploadStream(`${Date.now()}-${req?.file?.originalname}`, { contentType: req.file?.mimetype });
        const readableStream = new Readable();
        readableStream.push(req.file?.buffer);
        readableStream.push(null);
        readableStream.pipe(uploadFile);
        res.status(200).json({ message: 'success', success: true, filename: uploadFile.filename, id: uploadFile.id });
    } catch (error) {
        res.status(500).json({ message: error?.message, success: false });
    }
});
  1. File Uploading Process Explanation: In this API route, Multer middleware facilitates the parsing of file data originating from the front end. Subsequently, a GridFS bucket instance is established, and an upload stream is initialized using the openUploadStream method. The file's content type is defined, and the data is piped into the MongoDB database through a readable stream. The response includes crucial details such as the filename and unique identifier of the uploaded file.

Step 2: Retrieving Files from MongoDB GridFS

To retrieve files from the GridFS storage, a dynamic GET route is implemented, requiring the filename as a parameter.

route.get('/:filename', async (req, res) => {
    try {
        const file = await mongoose.connection.db.collection('fs.files').findOne({ filename: req.params?.filename });
        const gridFsBucket = new mongoose.mongo.GridFSBucket(mongoose.connection.db);
        const stream = gridFsBucket.openDownloadStream(file?._id);
        res.set('Content-Type', file?.contentType);
        stream.pipe(res);
    } catch (error) {
        res.status(500).json({ message: error?.message, success: false });
    }
});

Conclusion: In this comprehensive guide, we have meticulously outlined a proficient approach to uploading and retrieving files in the MongoDB GridFS storage system using your Express server. Through a step-by-step breakdown of the process, we have elucidated the integration of essential dependencies, the creation of API routes, and the utilization of MongoDB's GridFSBucket for seamless file management. By adhering to these guidelines, you can establish a robust file management system within your Express application. If you encounter any challenges during implementation or wish to explore advanced concepts such as video streaming directly from the GridFS bucket, please feel free to engage with us through the comments section. Your inquiries and interests are valued and appreciated.