Navigation

db.cloneCollection()

On this page

Definition

db.cloneCollection(from, collection, query)

Copies data directly between MongoDB instances. The db.cloneCollection() method wraps the cloneCollection database command and accepts the following arguments:

Parameter Type Description
from string The address of the server to clone from.
collection string

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 cloneCollection directly.

query document Optional. A standard query document that limits the documents copied as part of the db.cloneCollection() operation. All query selectors available to the find() are available here.

Behavior

mongos does not support db.cloneCollection().

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, db.cloneCollection() fails.

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

Changed in version 3.0: If the given collection already exists in the destination mongod instance, db.cloneCollection() returns an error.

Example

use users
db.cloneCollection('mongodb.example.net:27017', 'profiles',
                    { '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 }.