- Reference >
- Database Commands >
- Administration Commands >
- clone
clone¶
-
clone¶ The
clonecommand clones a database from amongodinstance to the currentmongodinstance.The command takes the following form:
cloneCollectionhas the following fields:Field Type Description hostnamestring The hostname of the database to copy. writeConcerndocument Optional. A document that expresses the write concern for the operation. Omit to use the default write concern. bypassDocumentValidationboolean Optional. Enables
cloneto 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
clonecommand, 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
mongoshell provides the methoddb.cloneDatabase()as a wrapper for theclonecommand.
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 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
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().