On this page
db.copyDatabase()
On this page
Definition
db.copyDatabase( fromdb, todb, fromhost, username, password, mechanism )- 
     
Changed in version 3.0: When authenticating to the
fromhostinstance,db.copyDatabase()supports SCRAM-SHA-1 and MONGODB-CR mechanisms to authenticate thefromhostuser.Copies a database either from one
mongodinstance to the currentmongodinstance or within the currentmongod.db.copyDatabase()wraps thecopydbcommand and takes the following arguments:Parameter Type Description fromdbstring Name of the source database. todbstring Name of the target database. fromhoststring Optional. The hostname of the source mongodinstance. Omit to copy databases within the samemongodinstance.usernamestring Optional. The name of the user on the
fromhostMongoDB instance. The user authenticates to thefromdb.For more information, see Authentication to Source mongod Instance.
passwordstring Optional. The password on the
fromhostfor authentication. The method does not transmit the password in plaintext.For more information, see Authentication to Source mongod Instance.
mechanismstring Optional. The mechanism to authenticate the
usernameandpasswordon thefromhost. Specify either:- SCRAM-SHA-1, or
 - MONGODB-CR (Deprecated in MongoDB 3.6).
 
db.copyDatabasedefaults to SCRAM-SHA-1 if the wire protocol version (maxWireVersion) is greater than or equal to3(i.e. MongoDB versions 3.0 or greater). Otherwise, it defaults to MONGODB-CR.Specify
MONGODB-CRto authenticate to the version 2.6.xfromhostfrom a version 3.0 instance or greater. For an example, see Copy Database from a mongod Instances that Enforce Authentication.New in version 3.0.
 
Behavior
Feature Compatibility Version
You cannot copy data between a mongod instance with featureCompatibilityVersion (FCV) 3.6 and a MongoDB version 3.4 and earlier mongod instance.
For example:
| Instance 1 | Instance 2 | |
|---|---|---|
Version 3.6 mongod with FCV 3.6 | 
        Version 3.6 mongod with FCV 3.6 | 
        Can copy data. | 
Version 3.6 mongod with FCV 3.6 | 
        Version 3.6 mongod with FCV 3.4 | 
        Can copy data. | 
Version 3.6 mongod with FCV 3.6 | 
        Version 3.4 mongod with FCV 3.4 | 
        Cannot copy data. Instance 2 must be a MongoDB version 3.6 | 
Version 3.6 mongod with FCV 3.4 | 
        Version 3.4 mongod with FCV 3.4 | 
        Can copy data. | 
Version 3.6 mongod with FCV 3.4 | 
        Version 3.2 mongod | 
        Can copy data. | 
Operations that copy data include:
db.cloneCollection()and the commandcloneCollectiondb.cloneDatabase()and the commandclonedb.copyDatabase()and the commandcopydb
Destination
- Run 
db.copyDatabase()in theadmindatabase of the destinationmongodinstance, i.e. the instance receiving the copied data. db.copyDatabase()creates the target database if it does not exist.- If the target database exists and no collection from the source database exists in the target database, 
db.copyDatabase()copies the collections from the source database to the target database. If any collection from the source database exists in the target database,db.copyDatabase()errors out and does not copy any remaining collections from the source database. db.copyDatabase()requires enough free disk space on the host instance for the copied database. Use thedb.stats()operation to check the size of the database on the sourcemongodinstance.
Authentication to Source mongod Instance
    If copying from another
mongodinstance (fromhost) that enforcesaccess control, then you must authenticate to thefromhostinstance by specifying theusername,password, and optionallymechanism. The method does not transmit the password in plaintext.When authenticating to the
fromhostinstance,db.copyDatabase()uses thefromdbas the authentication database for the specified user.When authenticating to the
fromhostinstance,db.copyDatabase()supports SCRAM-SHA-1 and MONGODB-CR mechanisms to authenticate thefromhostuser.- To authenticate to a version 2.6 
fromhost, you must specifyMONGODB-CRas the authentication mechanism. See Copy Database from a mongod Instances that Enforce Authentication. - To copy from a version 3.0 
fromhostto a version 2.6 instance, i.e. if running the method from a version 2.6 instance to copy from a version 3.0fromhost, you can only authenticate to thefromhostas aMONGODB-CRuser. 
- To authenticate to a version 2.6 
 
For more information on required access and authentication, see Required Access.
Concurrency
db.copyDatabase()andclonedo not produce point-in-time snapshots of the source database. Write traffic to the source or destination database during the copy process will result in divergent data sets.db.copyDatabase()does not lock the destination server during its operation, so the copy will occasionally yield to allow other operations to complete.
Indexes
MongoDB performs foreground builds of indexes on databases copied via db.copyDatabase(). Foreground index builds lock the database and prevent all other operations on that database until the foreground build completes. There may also be a performance impact on other databases while the indexes build.
You can keep track of ongoing index creation operations with the db.currentOp() command.
Required Access
Changed in version 2.6.
Source Database (fromdb)
    If the mongod instance of the source database (fromdb) enforces access control, you must have proper authorization for the source database.
If copying from another mongod instance (fromhost) that enforces access control, then you must authenticate to the fromhost instance by specifying the username, password, and optionally mechanism. The method does not transmit the password in plaintext.
When authenticating to the fromhost instance, db.copyDatabase() uses the fromdb as the authentication database for the specified user.
When authenticating to the fromhost instance, db.copyDatabase() supports SCRAM-SHA-1 and MONGODB-CR mechanisms to authenticate the fromhost user.
- To authenticate to a version 2.6 
fromhost, you must specifyMONGODB-CRas the authentication mechanism. See Copy Database from a mongod Instances that Enforce Authentication. - To copy from a version 3.0 
fromhostto a version 2.6 instance, i.e. if running the method from a version 2.6 instance to copy from a version 3.0fromhost, you can only authenticate to thefromhostas aMONGODB-CRuser. 
Source is non-admin Database
     Changed in version 3.0.
If the source database is a non-admin database, you must have privileges that specify find, listCollections, and listIndexes actions on the source database, and find action on the system.js collection in the source database.
{ resource: { db: "mySourceDB", collection: "" }, actions: [ "find", "listCollections", "listIndexes" ] },
{ resource: { db: "mySourceDB", collection: "system.js" }, actions: [ "find" ] },
       Source is admin Database
     Changed in version 3.0.
If the source database is the admin database, you must have privileges that specify find, listCollections, and listIndexes actions on the admin database, and find action on the system.js, system.users, system.roles, and system.version collections in the admin database. For example:
{ resource: { db: "admin", collection: "" }, actions: [ "find",  "listCollections", "listIndexes" ]