- Reference >
- Database Commands >
- cloneCollection
cloneCollection¶
-
cloneCollection¶ The
cloneCollectioncommand copies a collection from a remote server to the server where you run the command.cloneCollectiondoes not allow you to clone a collection through amongos: you must connect directly to themongodinstance.Parameters: - from – Specify a resolvable hostname, and optional port number of the remote server where the specified collection resides.
- query – Optional. A query document, in the form of a document,
that filters the documents in the remote collection that
cloneCollectionwill copy to the current database. Seedb.collection.find(). - copyIndexes (Boolean) – Optional.
trueby default. When set tofalsethe indexes on the originating server are not copied with the documents in the collection.
Consider the following example:
This operation copies the
profilescollection from theusersdatabase on the server atmongodb.example.net. The operation only copies documents that satisfy the query{ active: true }and does not copy indexes.cloneCollectioncopies indexes by default, but you can disable this behavior by setting{ copyIndexes: false }. ThequeryandcopyIndexesarguments are optional.cloneCollectioncreates a collection on the current database with the same name as the origin collection. If, in the above example, theprofilescollection already exists in the local database, then MongoDB appends documents in the remote collection to the destination collection.