0
0
MongodbHow-ToBeginner · 4 min read

How to Upload Files to MongoDB Easily

To upload a file to MongoDB, use GridFS, a specification for storing large files. You can upload files by splitting them into chunks and storing metadata using MongoDB drivers or tools like mongofiles.
📐

Syntax

Uploading files to MongoDB uses GridFS, which stores files in two collections: fs.files for metadata and fs.chunks for file data chunks.

Basic steps include:

  • Open the file to upload.
  • Create a GridFS bucket.
  • Upload the file stream to the bucket.
javascript
const { MongoClient, GridFSBucket } = require('mongodb');
const fs = require('fs');

async function uploadFile(uri, dbName, filePath, fileName) {
  const client = new MongoClient(uri);
  await client.connect();
  const db = client.db(dbName);
  const bucket = new GridFSBucket(db);

  const uploadStream = bucket.openUploadStream(fileName);
  const readStream = fs.createReadStream(filePath);

  readStream.pipe(uploadStream)
    .on('error', (error) => console.error('Upload error:', error))
    .on('finish', () => {
      console.log('File uploaded successfully');
      client.close();
    });
}
💻

Example

This example shows how to upload a local file named example.txt to MongoDB using Node.js and GridFS.

javascript
const { MongoClient, GridFSBucket } = require('mongodb');
const fs = require('fs');

async function uploadFile() {
  const uri = 'mongodb://localhost:27017';
  const client = new MongoClient(uri);
  try {
    await client.connect();
    const db = client.db('myfilesdb');
    const bucket = new GridFSBucket(db);

    const uploadStream = bucket.openUploadStream('example.txt');
    const readStream = fs.createReadStream('example.txt');

    readStream.pipe(uploadStream)
      .on('error', (error) => console.error('Upload error:', error))
      .on('finish', () => {
        console.log('File uploaded successfully');
        client.close();
      });
  } catch (err) {
    console.error(err);
  }
}

uploadFile();
Output
File uploaded successfully
⚠️

Common Pitfalls

  • Not using GridFS for large files: MongoDB documents have a size limit (~16MB), so large files must use GridFS.
  • Forgetting to handle streams properly: Always listen for error and finish events on streams.
  • Not closing the database connection: Always close the client after upload to avoid resource leaks.
  • Incorrect file path or permissions: Ensure the file exists and your app has read access.
javascript
/* Wrong way: Uploading large file as a single document (fails if >16MB) */
// db.collection('files').insertOne({ name: 'largefile', data: fs.readFileSync('largefile.bin') });

/* Right way: Use GridFS to handle large files */
// Use GridFSBucket as shown in the example section.
📊

Quick Reference

Remember these key points when uploading files to MongoDB:

  • Use GridFS for files larger than 16MB.
  • Use GridFSBucket from your MongoDB driver.
  • Stream files to avoid loading entire file into memory.
  • Handle error and finish events on streams.
  • Close the database connection after upload.

Key Takeaways

Use GridFS to upload files larger than 16MB to MongoDB.
Stream files with GridFSBucket to efficiently upload without memory issues.
Always handle stream events and close the database connection.
Check file paths and permissions before uploading.
Avoid storing large files directly in documents to prevent errors.