Navigation
This version of the documentation is archived and no longer supported.

db.copyDatabase()

Definition

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

Changed in version 3.0: When authenticating to the fromhost instance, db.copyDatabase() supports MONGODB-CR and SCRAM-SHA-1 mechanisms to authenticate the fromhost user.

Copies a database either from one mongod instance to the current mongod instance or within the current mongod. db.copyDatabase() wraps the copydb command and takes the following arguments:

Parameter Type Description
fromdb string Name of the source database.
todb string Name of the target database.
fromhost string Optional. The hostname of the source mongod instance. Omit to copy databases within the same mongod instance.
username string

Optional. The name of the user on the fromhost MongoDB instance. The user authenticates to the fromdb.

For more information, see Authentication to Source mongod Instance.

password string

Optional. The password on the fromhost for authentication. The method does not transmit the password in plaintext.

For more information, see Authentication to Source mongod Instance.

mechanism string

Optional. The mechanism to authenticate the username and password on the fromhost. Specify either MONGODB-CR or SCRAM-SHA-1.

db.copyDatabase defaults to SCRAM-SHA-1 if the wire protocol version (maxWireVersion) is greater than or equal to 3 (i.e. MongoDB versions 3.0 or greater). Otherwise, it defaults to MONGODB-CR.

Specify MONGODB-CR to authenticate to the version 2.6.x fromhost from 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

Destination

  • Run db.copyDatabase() in the admin database of the destination mongod instance, i.e. the instance receiving the copied data.
  • db.copyDatabase() creates the target database if it does not exist.
  • db.copyDatabase() requires enough free disk space on the host instance for the copied database. Use the db.stats() operation to check the size of the database on the source mongod instance.

Authentication to Source mongod Instance

  • 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 MONGODB-CR and SCRAM-SHA-1 mechanisms to authenticate the fromhost user.

    • To authenticate to a version 2.6 fromhost, you must specify MONGODB-CR as the authentication mechanism. See Copy Database from a mongod Instances that Enforce Authentication.
    • To copy from a version 3.0 fromhost to a version 2.6 instance, i.e. if running the method from a version 2.6 instance to copy from a version 3.0 fromhost, you can only authenticate to the fromhost as a MONGODB-CR user.

For more information on required access and authentication, see Required Access.

Concurrency

  • db.copyDatabase() and clone do 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.

Sharded Clusters

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 MONGODB-CR and SCRAM-SHA-1 mechanisms to authenticate the fromhost user.

  • To authenticate to a version 2.6 fromhost, you must specify MONGODB-CR as the authentication mechanism. See Copy Database from a mongod Instances that Enforce Authentication.
  • To copy from a version 3.0 fromhost to a version 2.6 instance, i.e. if running the method from a version 2.6 instance to copy from a version 3.0 fromhost, you can only authenticate to the fromhost as a MONGODB-CR user.

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" ] },
{ resource: { db: "admin", collection: "system.js" }, actions: [ "find" ] },
{ resource: { db: "admin", collection: "system.users" }, actions: [ "find" ] },
{ resource: { db: "admin", collection: "system.roles" }, actions: [ "find" ] },
{ resource: { db: "admin", collection: "system.version" }, actions: [ "find" ] }

Target Database (todb)

If the mongod instance of the target database (todb) enforces access control, you must have proper authorization for the target database.

Copy from non-admin Database

If the source database is not the admin database, you must have privileges that specify insert and createIndex actions on the target database, and insert action on the system.js collection in the target database. For example:

{ resource: { db: "myTargetDB", collection: "" }, actions: [ "insert", "createIndex" ] },
{ resource: { db: "myTargetDB", collection: "system.js" }, actions: [ "insert" ] }

Copy from admin Database

If the source database is the admin database, you must have privileges that specify insert and createIndex actions on the target database, and insert action on the system.js, system.users, system.roles, and system.version collections in the target database. For example:

{ resource: { db: "myTargetDB", collection: "" }, actions: [ "insert", "createIndex" ] },
{ resource: { db: "myTargetDB", collection: "system.js" }, actions: [ "insert" ] },
{ resource: { db: "myTargetDB", collection: "system.users" }, actions: [ "insert" ] },
{ resource: { db: "myTargetDB", collection: "system.roles" }, actions: [ "insert" ] },
{ resource: { db: "myTargetDB", collection: "system.version" }, actions: [ "insert" ] }

Example

Copy from the Same mongod Instance

To copy within the same mongod instance, omit the fromhost.

The following operation copies a database named records into a database named archive_records:

db.copyDatabase('records', 'archive_records')

Copy Database from a mongod Instances that Enforce Authentication

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.

Changed in version 3.0: MongoDB 3.0 supports passing the authentication mechanism to use for the fromhost.

The following operation copies a database named reporting from a version 2.6 mongod instance that runs on example.net and enforces access control.

 db.copyDatabase(
   "reporting",
   "reporting_copy",
   "example.net",
   "reportUser",
   "abc123",
   "MONGODB-CR"
)

See also

clone