On this page
clone
clone
-
The
clone
command clones a database from amongod
instance to the currentmongod
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 methoddb.cloneDatabase()
as a wrapper for theclone
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 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 commandcloneCollection
db.cloneDatabase()
and the commandclone
db.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()
.
use marketing
db.runCommand( { clone: "db1.example.net:27017" })