- Reference >
mongoShell Methods >- Database Methods >
- db.cloneCollection()
db.cloneCollection()¶
On this page
Definition¶
-
db.cloneCollection(from, collection, query)¶ Deprecated since version 4.2: MongoDB deprecates
cloneCollectionand its helperdb.cloneCollection(). For alternatives, see Deprecate Support for cloneCollection.Copies data directly between MongoDB instances. The
db.cloneCollection()method wraps thecloneCollectioncommand and accepts the following arguments:Parameter Type Description fromstring The address of the server to clone from. collectionstring The collection in the MongoDB instance that you want to copy.
db.cloneCollection()will only copy the collection with this name from database of the same name as the current database the remote MongoDB instance.If you want to copy a collection from a different database name you must use the
cloneCollectiondirectly.querydocument Optional. A standard query document that limits the documents copied as part of the db.cloneCollection()operation. All query selectors available to thefind()are available here.
Behavior¶
mongos¶
mongos does not support db.cloneCollection().
Namespace¶
When copying the specified collection, db.cloneCollection() uses the same
database name in the from server as the destination database. If the
collection does not exist in the source database, db.cloneCollection() fails.
If the given collection already exists in the destination
mongod instance, db.cloneCollection() returns an
error.
Authorization¶
db.cloneCollection() cannot be used if the from server has
authorization enabled.
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 commandcloneCollection- db.cloneDatabase() and the command clone
- db.copyDatabase() and the command copydb
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 }.