How to copy a Database from a MongoDB instance to another?

While recently i was working in an application i had to copy the database from a mongoDB  hosted in an mlab instance to my local. I will share an easy step in MongoDB Shell which support to copy database from remote instance to current one with a single command.
DEMO with steps :
I made  two instances of MongoDB from following commands.
 //Instance 1  
mongod --port 9998 --dbpath /data/db1
//Instance 2
mongod --port 9999 --dbpath /data/db2
In instance 1 there is a database called “dsampledb1“.
and i started the instance with the following command,

 mongo localhost:9998  
I create a database with one collection with the following command,
 use dsampledb1  
db.users.save({id:1, name:"sample name"})
Then I log in to next MongoDB instance using MongoDB Shell.
 mongo localhost:9999  
Now to copy the database from the instance 1 to instance 2 , we can simply use the following command,
 db.copyDatabase("dsampledb1","dsampledb2","localhost:9999")  
Syntax is as follows,
 db.copyDatabase(sourcedb, destinationdb, fromhost, username, password)  

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.