Navigation

cloneCollection

On this page

Definition

cloneCollection

Copies a collection from a remote mongod instance to the current mongod instance. cloneCollection creates a collection in a database with the same name as the remote collection’s database. cloneCollection takes the following form:

Changed in version 3.6.

{
  cloneCollection: "<namespace>",
  from: "<hostname>",
  query: { <query> },
  txnNumber: <long>,
  lsid: { id: <UUID> }
}

cloneCollection has the following fields:

Field Type Description
cloneCollection string The namespace of the collection to copy. The namespace is a combination of the database name and the name of the collection.
from string The address of the server to clone from.
query document Optional. A query that filters the documents in the source collection that cloneCollection will copy to the current database.
txnNumber 64-bit integer/long

Optional.

A strictly increasing non-negative number that uniquely identifies the command in the command’s session.

If specified, the command must also include the lsid option.

Tip

The mongo shell and the drivers provide helpers to start the session and assign a txnNumber to the commands in the session.

New in version 3.6.

lsid document

Optional.

The document that specifies the unique id of the session associated with the command. If the txnNumber is specified, the lsid is required.

Tip

The mongo shell and the drivers provide helpers to start the session and assign the lsid for the commands in the session.

New in version 3.6.

Behavior

mongos does not support cloneCollection.

cloneCollection cannot be used if the from server has authorization enabled.

Changed in version 3.0: If the given namespace already exists in the destination mongod instance, cloneCollection will return an error.

Example

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

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 }.