- 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:{ cloneCollection: "<namespace>", from: "<hostname>", query: { <query> } }
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.
Behavior¶
mongos does not support cloneCollection.
cloneCollection cannot be used if the from server has
authorization enabled.
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 }.