MongoDB Copy Database

MongoDB Copy Database

MongoDB Copy Database

Copying a database in MongoDB is useful for backups, migrations, or creating test environments. This guide explains how to copy a database using MongoDB Shell, mongodump and mongorestore, and Node.js (Mongoose).

1. Copy a Database Using MongoDB Shell

MongoDB does not have a built-in copyDatabase() function in modern versions. Instead, you must manually copy collections.

A. Open MongoDB Shell

Start the MongoDB shell:

mongosh

B. Select the Source Database

use sourceDB

C. Copy Each Collection to the Target Database

To copy the posts collection from sourceDB to targetDB:

db.posts.aggregate([{ $out: "targetDB.posts" }])

Verify the copy:

use targetDB show collections

Output:

posts

The the posts collection has been copied to targetDB.

2. Copy a Database Using mongodump and mongorestore

This method exports the database and then restores it into a new one.

A. Dump (Export) the Source Database

mongodump --db=sourceDB --out=/backup/

This creates a backup in /backup/sourceDB/.

B. Restore (Import) to a New Database

mongorestore --db=targetDB /backup/sourceDB/

Verify the copy:

mongosh use targetDB show collections

Expected Output:

posts users comments

3. Copy a Database Using Node.js (Mongoose)

A. Install Dependencies

npm install mongoose

B. Create a Script to Copy All Collections

Create copyDatabase.js:

const mongoose = require('mongoose'); const sourceDB = 'mongodb://127.0.0.1:27017/sourceDB'; const targetDB = 'mongodb://127.0.0.1:27017/targetDB'; async function copyCollections() { try { const srcConn = await mongoose.createConnection(sourceDB, { useNewUrlParser: true, useUnifiedTopology: true }); const tgtConn = await mongoose.createConnection(targetDB, { useNewUrlParser: true, useUnifiedTopology: true }); const collections = await srcConn.db.listCollections().toArray(); for (let collection of collections) { const docs = await srcConn.db.collection(collection.name).find().toArray(); if (docs.length) { await tgtConn.db.collection(collection.name).insertMany(docs); console.log(`✅ Copied collection: ${collection.name}`); } } srcConn.close(); tgtConn.close(); console.log("🎉 Database copy complete!"); } catch (error) { console.error("❌ Error copying database:", error.message); } } copyCollections();

C. Run the Script

node copyDatabase.js

Output:

Copied collection: posts Copied collection: users Copied collection: comments 🎉 Database copy complete!

4. Summary

Copied collections manually using MongoDB Shell ($out).
Used mongodump and mongorestore for full database copying.
Created a Node.js script to copy all collections programmatically.

5. Next Steps

🔹 Schedule automatic backups for better data safety.
🔹 Use authentication when copying databases between servers.
🔹 Optimize large database copies using indexes.

Would you like help setting up automated backups or cloud migrations? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close