- Reference >
- Database Commands >
- Administration Commands >
- cloneCollection
cloneCollection¶
On this page
Definition¶
-
cloneCollection¶ Copies a collection from a remote
mongodinstance to the currentmongodinstance.cloneCollectioncreates a collection in a database with the same name as the remote collection’s database.cloneCollectiontakes the following form:cloneCollectionhas the following fields:Field Type Description cloneCollectionstring The namespace of the collection to copy. The namespace is a combination of the database name and the name of the collection. fromstring The address of the server to clone from. querydocument Optional. A query that filters the documents in the source collection that cloneCollectionwill copy to the current database.writeConcerndocument Optional. A document that expresses the write concern for the operation. Omit to use the default write concern. bypassDocumentValidationboolean Optional. Enables
cloneCollectionto bypass document validation during the operation. This lets you insert documents that do not meet the validation requirements.New in version 3.2.
The mongo shell provides the method
db.cloneCollection()as a wrapper for thecloneCollectioncommand.
Behavior¶
mongos¶
mongos does not support cloneCollection.
Authorization¶
cloneCollection cannot be used if the from server has
authorization enabled.
Namespace¶
Changed in version 3.0.
If the given namespace already exists in the destination
mongod instance, cloneCollection will return an
error.
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:
db.cloneCollection()and the commandcloneCollectiondb.cloneDatabase()and the commandclonedb.copyDatabase()and the commandcopydb
Example¶
This operation copies the profiles collection from the users
database on the server at mongodb.example.net into the users
database on the local server. The operation only
copies documents that satisfy the query { 'active' : true }.