MongoDB provides the copydb and clone database commands to support migrations of entire logical databases between mongod instances. With these commands you can copy data between instances with a simple interface without the need for an intermediate stage. The db.cloneDatabase() and db.copyDatabase() provide helpers for these operations in the mongo shell. These migration helpers run the commands on the destination server and pull data from the source server..
Data migrations that require an intermediate stage or that involve more than one database instance are beyond the scope of this tutorial. copydb and clone are more ideal for use cases that resemble the following use cases:
Use the following procedure to copy the database named test on server db0.example.net to the server named db1.example.net and rename it to records in the process:
Verify that the database, test exists on the source mongod instance running on the db0.example.net host.
Connect to the destination server, running on the db1.example.net host, using the mongo shell.
Model your operation on the following command:
db.copyDatabase( "test", "records", "db0.example.net" )
Use the following procedure to rename the test database records on a single mongod instance:
In the following operation, you will copy the test database from the mongod running on db0.example.net to the records database on the local instance (e.g. db1.example.net.) Because the mongod instance running on db0.example.net requires authentication for all connections, you will need to pass db.copyDatabase() authentication credentials, as in the following procedure:
Issue the following command:
db.copyDatabase( "test", "records", "db0.example.net", "<username>", "<password>")
Replace <username> and <password> with your authentication credentials.
For many operations, clone is functionally equivalent to copydb, but it has a more simple syntax and a more narrow use. The mongo shell provides the db.cloneDatabase() helper as a wrapper around clone.