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

clone

On this page

clone

Deprecated since version 4.0: MongoDB deprecates clone and its helper db.cloneDatabase(). For information on alternatives, see copydb and clone Commands.

The clone command clones a database from a mongod instance to the current mongod instance.

The command takes the following form:

{ clone: <copyfromhost:port>, writeConcern: <document>, bypassDocumentValidation: <boolean> }

cloneCollection has the following fields:

Field Type Description
hostname string The hostname of the database to copy.
writeConcern document Optional. A document that expresses the write concern for the operation. Omit to use the default write concern.
bypassDocumentValidation boolean

Optional. Enables clone to bypass document validation during the operation. This lets you insert documents that do not meet the validation requirements.

New in version 3.2.

To use the clone command, run the command on the destination server, specifying the source to copy from. The database where you run the command determines which database to copy from the source.

The mongo shell provides the method db.cloneDatabase() as a wrapper for the clone command.

Behavior

Data

The clone command does not snapshot the database. If any clients update the database you’re copying at any point during the clone operation, the resulting database may be inconsistent.

Locks

The destination database will be locked periodically during the clone operation. In other words, clone will occasionally yield to allow other operations on the database to complete.

FeatureCompatibilityVersion

You cannot copy data between a MongoDB 4.0 mongod instance with featureCompatibilityVersion (FCV) 4.0 and a MongoDB version 3.6 mongod instance.

Note

You cannot copy data between a MongoDB 4.0 mongod instance (regardless of the FCV value) and a MongoDB 3.4 and earlier mongod instance.

For example:

Instance 1 Instance 2  
Version 4.0 mongod with FCV 4.0 Version 4.0 mongod with FCV 4.0 Can copy data.
Version 4.0 mongod with FCV 4.0 Version 4.0 mongod with FCV 3.6 Can copy data.
Version 4.0 mongod with FCV 4.0 Version 3.6 mongod with FCV 3.6 Cannot copy data. Instance 2 must be a MongoDB version 4.0
Version 4.0 mongod with FCV 3.6 Version 3.6 mongod with FCV 3.6 Can copy data.
Version 4.0 mongod with FCV 3.6 Version 3.6 mongod with FCV 3.4 Can copy data.

In general, if the mongod instance has its featureCompatibilityVersion (FCV) set to its MongoDB version, you cannot copy data between that instance and a mongod instance of an earlier MongoDB version.

Operations that copy data include:

Replica Sets

The clone command can copy from a secondary member of a replica set.

Example

The following operation copies the marketing database from "db1.example.net:27017" to the mongod instance where you issue the db.runCommand().

use marketing
db.runCommand( { clone: "db1.example.net:27017" })