cloneCollection

Definition

  • cloneCollection

Deprecated since version 4.2: MongoDB deprecates cloneCollection and its helperdb.cloneCollection(). For alternatives, seeDeprecate Support for cloneCollection.

Copies a collection from a remote mongod instance to thecurrent mongod instance. cloneCollectioncreates a collection in a database with the same name as the remotecollection’s database. cloneCollection takes thefollowing form:

  1. { cloneCollection: "<namespace>",
  2. from: "<hostname>",
  3. query: { <query> },
  4. writeConcern: { <write concern> }
  5. }

cloneCollection has the following fields:

FieldTypeDescriptioncloneCollectionstringThe namespace of the collection to copy. The namespace is acombination of the database name and the name of the collection.fromstringThe address of the server to clone from.querydocumentOptional. A query that filters the documents in the source collection thatcloneCollection will copy to the current database.writeConcerndocumentOptional. A document that expresses the write concernfor the operation. Omit to use the default writeconcern.

The mongo shell provides the methoddb.cloneCollection() as a wrapper for thecloneCollection command.

Behavior

mongos

mongos does not support cloneCollection.

Authorization

cloneCollection cannot be used if the from server hasauthorization enabled.

Namespace

Changed in version 3.0.

If the given namespace already exists in the destinationmongod instance, cloneCollection will return anerror.

FeatureCompatibilityVersion

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

Note

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

For example:

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

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

Operations that copy data include:

Example

  1. db.getSiblingDB("users").runCommand( { cloneCollection: "users.profiles",
  2. from: "mongodb.example.net:27017",
  3. query: { 'active' : true }
  4. } )

This operation copies the profiles collection from the usersdatabase on the server at mongodb.example.net into the usersdatabase on the local server. The operation onlycopies documents that satisfy the query { 'active' : true }.