mongodbcopy is a developer-friendly CLI tool to copy, export, import, or back up MongoDB collections and databases - safely, quickly, and locally - without complex MongoDB shell commands.
- πͺ Simple CLI - Copy databases or specific collections in one line
- β‘ Fast batch copying - Adjustable batch size for huge datasets
- π§° Dry-run mode - Simulate copy before actually writing
- πΎ JSON Export/Import - Backup or restore collections as JSON files
- π€ CI-ready - Use
--yesto skip confirmations in scripts - π Progress feedback - Real-time progress bars with document counts
- π Incremental backups - Copy only new/updated documents since last backup
- π Index copying - Automatically copy indexes from source to target
- β Schema validation - Validate data compatibility before copying
- π Performance optimized - Streaming and bulk operations for large datasets
- π§ Environment-based config - Works out of the box via
.env
npm install -g mongodbcopynpm install mongodbcopy# Clone from GitHub
git clone https://github.com/iamdhiraj69/mongodbcopy.git
cd mongodbcopy
npm install
# Link globally for testing
npm linkCopy .env.example β .env and update your MongoDB details:
SOURCE_DB_URI=mongodb+srv://username:password@source.mongodb.net
TARGET_DB_URI=mongodb+srv://username:password@target.mongodb.net
DB_NAME=my_databasemongodbcopy --allmongodbcopy --collections users,postsmongodbcopy --all --dry-runmongodbcopy --all --batch-size 500mongodbcopy --all --yesmongodbcopy --all --copy-indexes# Copy documents updated in the last 7 days
mongodbcopy --all --incremental --timestamp-field updatedAt --since 2024-01-01T00:00:00Zmongodbcopy --all --validate-schemamongodbcopy --all --no-progressmongodbcopy --all --export-jsonAll files will be saved to the backup/ folder (auto-created).
mongodbcopy --import-jsonYou can change the backup directory using:
mongodbcopy --export-json --output-dir ./my_backup| Key | Description | Default |
|---|---|---|
| SOURCE_DB_URI | MongoDB source URI | Required |
| TARGET_DB_URI | MongoDB target URI | Required |
| DB_NAME | Database name | Required |
| BATCH_SIZE | Documents per insert batch | 1000 |
| LOG_TO_FILE | Write logs to file (true/false) | false |
| LOG_PATH | Log file path (if enabled) | ./mongodbcopy.log |
| BACKUP_DIR | JSON export/import folder | ./backup |
mongodbcopy --collections users,posts --batch-size 2000 --yesCopies only users and posts collections using batch size 2000 without confirmation.
mongodbcopy --all --copy-indexes --export-json --output-dir ./full-backupExports all collections and their indexes to JSON files.
mongodbcopy --all --incremental --timestamp-field updatedAt --since 2024-01-01T00:00:00ZCopies only documents updated since January 1, 2024.
mongodbcopy --all --validate-schema --copy-indexes --batch-size 5000Validates schema compatibility and copies with indexes using larger batches.
Use mongodbcopy in your Node.js applications:
import { copyCollections } from 'mongodbcopy';
// Copy specific collections
const results = await copyCollections({
sourceUri: 'mongodb://localhost:27017',
targetUri: 'mongodb://localhost:27018',
dbName: 'myDatabase',
collections: ['users', 'posts'],
batchSize: 1000,
dryRun: false,
showProgress: true
});
console.log(results);
// [
// { name: 'users', copied: 1500, total: 1500, status: 'copied' },
// { name: 'posts', copied: 3200, total: 3200, status: 'copied' }
// ]
// Export to JSON with indexes
const exportResults = await copyCollections({
sourceUri: 'mongodb://localhost:27017',
targetUri: 'mongodb://localhost:27017',
dbName: 'myDatabase',
collections: ['users'],
exportJson: true,
outputDir: './backup',
copyIndexes: true
});
// Incremental backup (only documents updated since a date)
const incrementalResults = await copyCollections({
sourceUri: 'mongodb://localhost:27017',
targetUri: 'mongodb://localhost:27018',
dbName: 'myDatabase',
collections: ['users'],
incremental: true,
timestampField: 'updatedAt',
since: new Date('2024-01-01'),
showProgress: true
});
// Copy with schema validation
const validatedResults = await copyCollections({
sourceUri: 'mongodb://localhost:27017',
targetUri: 'mongodb://localhost:27018',
dbName: 'myDatabase',
collections: ['users'],
validateSchema: true,
copyIndexes: true
});npm install
npm run startTo use it as a global CLI after publishing, add this to package.json:
{
"bin": {
"mongodbcopy": "./src/index.js"
}
}Then install globally:
npm i -g .
mongodbcopy --help| Status | Enhancement | Description |
|---|---|---|
| β | --dry-run | Simulate copy without writing |
| β | --collections | Copy specific collections |
| β | JSON export/import | Backup & restore to local JSON |
| β | --yes flag | Skip confirmation for CI |
| β | Progress bars | Real-time visual feedback with document counts |
| β | Index copying | Copy indexes from source to target |
| β | Incremental backups | Copy only new/updated documents |
| β | Schema validation | Validate before copying |
| β | Performance optimizations | Streaming and bulk operations |
| β | Enhanced test coverage | Comprehensive test cases |
| π§ | File logging | Save logs for debugging |
| π§© | TypeScript version | Optional future version |
Dhiraj
π¦ GitHub: iamdhiraj69