w3resource

MongoDB: db.copyDatabase() method

db.copyDatabase

The db.copyDatabase method us used to copy a database either from one mongod instance to the current mongod instance or within the current mongod. 

Syntax:

db.copyDatabase(fromdb, todb, fromhost, username, password, mechanism)

Parameters:

Name Description Required /
Optional
Type
fromdb Name of the source database. Required string
todb Name of the target database. Required string
fromhost The hostname of the source mongod instance. Omit to copy databases within the same mongod instance. Optional string
username The name of the user on the fromhost MongoDB instance. The user authenticates to the fromdb. Optional string

Example: MongoDB: db.copyDatabase() method

The following command will display the help text of command findAndModify.

Here is the sample databases

> show dbs
admin    0.078GB
local    0.078GB
myinfo   0.078GB
payroll  0.078GB
test     0.078GB
db.copyDatabase("test","newtest");

Output:

> db.copyDatabase("test","newtest");
{ "ok" : 1 }

To see the newly created database use the following command.

> show dbs
admin    0.078GB
local    0.078GB
myinfo   0.078GB
<span class="style1">newtest  0.078GB</span>
payroll  0.078GB
test     0.078GB

The see the collections within the newly created database the following command can be used. 

> use newtest
switched to db newtest

> show collections;
empdetails
invoice
orders
prod_mast
prod_master
restaurants
restaurants_new
system.indexes
userdetails

Retrieve the restaurants data from here

Previous: db.commandHelp() method
Next: db.createCollection() method



Follow us on Facebook and Twitter for latest update.