NodeJS实现MongoDB数据压缩备份为JSON文件的脚本开发需求
Hey there! I see you're new to Node.js and trying to back up your MongoDB data as JSON instead of bulky BSON files—great call on wanting to cut down on backup size. Let's walk through how to fix this, whether you want to tweak your existing script or build something more flexible from scratch.
Option 1: Adjust Your Existing mongodb-backup Script
It looks like you're using the mongodb-backup module in your code. By default, this module exports BSON, but you can easily configure it to output JSON instead by adding the parser: 'json' option. Here's how to update that part of your script:
const backup = require('mongodb-backup'); // Add this configuration to your backup call backup({ uri: 'mongodb://localhost:27017/your-db-name', // Your MongoDB connection string root: './backup', // Local backup directory parser: 'json', // This forces JSON output instead of BSON // ... your other existing config (like AWS S3 settings if needed) });
This will generate JSON files for your collections instead of the larger BSON binaries.
Option 2: Build a Custom Backup Script (More Flexible)
If you want full control over how your data is serialized (like handling MongoDB-specific types like ObjectId or Date), building a custom script with the official MongoDB Node.js driver is a better bet. Here's a complete example that exports all collections to a single JSON file and can upload it to S3:
First, install the required dependencies:
npm install mongodb aws-sdk
Then the script:
'use strict'; const { MongoClient } = require('mongodb'); const fs = require('fs').promises; const path = require('path'); const AWS = require('aws-sdk'); // -------------------------- // Configuration // -------------------------- const MONGO_URI = 'mongodb://localhost:27017/your-database-name'; const BACKUP_FILE_PATH = path.join(__dirname, 'db-backup.json'); const S3_CONFIG = { accessKeyId: 'your-access-key-id', secretAccessKey: 'your-secret-access-key', region: 'your-aws-region' }; const S3_BUCKET_NAME = 'your-s3-bucket-name'; const S3_FILE_KEY = 'backups/db-backup.json'; // -------------------------- // Helper: Serialize MongoDB Docs to JSON-Friendly Format // -------------------------- function serializeMongoDocument(doc) { // Convert ObjectId to plain string if (doc._id) doc._id = doc._id.toString(); // Convert Dates to ISO strings (human-readable and JSON-compatible) Object.keys(doc).forEach(key => { if (doc[key] instanceof Date) { doc[key] = doc[key].toISOString(); } }); return doc; } // -------------------------- // Core Backup Function // -------------------------- async function backupMongoToJson() { let mongoClient; try { // Connect to MongoDB mongoClient = await MongoClient.connect(MONGO_URI); const db = mongoClient.db(); // Get all collections in the database const collections = await db.listCollections().toArray(); const backupData = {}; // Fetch data from each collection for (const coll of collections) { const collection = db.collection(coll.name); const documents = await collection.find({}).toArray(); backupData[coll.name] = documents.map(serializeMongoDocument); } // Write JSON to local file const jsonContent = JSON.stringify(backupData, null, 2); // Remove null,2 for minified (smaller size) await fs.writeFile(BACKUP_FILE_PATH, jsonContent, 'utf8'); console.log(`Local backup saved to: ${BACKUP_FILE_PATH}`); // Upload to S3 (skip this if you don't need cloud storage) await uploadToS3(BACKUP_FILE_PATH, S3_FILE_KEY); console.log('Backup uploaded to S3 successfully!'); } catch (error) { console.error('Backup failed:', error.message); } finally { // Close MongoDB connection if (mongoClient) await mongoClient.close(); } } // -------------------------- // Helper: Upload File to S3 // -------------------------- async function uploadToS3(filePath, s3Key) { const s3 = new AWS.S3(S3_CONFIG); const fileContent = await fs.readFile(filePath); const uploadParams = { Bucket: S3_BUCKET_NAME, Key: s3Key, Body: fileContent }; await s3.upload(uploadParams).promise(); } // Run the backup backupMongoToJson();
Bonus: Compress the JSON for Even Smaller Size
JSON is already smaller than BSON, but you can shrink it further by compressing it into a .gz file. Here's how to modify the custom script to generate a compressed backup:
Add the zlib module (built into Node.js, no need to install) and update the write step:
const zlib = require('zlib'); const { pipeline } = require('stream/promises'); // Replace the fs.writeFile part with this: async function writeCompressedJson(data, outputPath) { const jsonString = JSON.stringify(data); const gzipStream = zlib.createGzip(); const writeStream = fs.createWriteStream(outputPath); await pipeline( require('stream').Readable.from(jsonString), gzipStream, writeStream ); } // Usage in backupMongoToJson: await writeCompressedJson(backupData, path.join(__dirname, 'db-backup.json.gz'));
This compressed file will be significantly smaller than both raw JSON and BSON—perfect for saving storage space!
内容的提问来源于stack exchange,提问作者Neelam Soni

