- Reference >
- Database Commands >
- cloneCollection
cloneCollection¶
-
cloneCollection
¶ The
cloneCollection
command copies a collection from a remote server to the server where you run the command.cloneCollection
does not allow you to clone a collection through amongos
: you must connect directly to themongod
instance.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
cloneCollection
will copy to the current database. Seedb.collection.find()
. - copyIndexes (Boolean) – Optional.
true
by default. When set tofalse
the indexes on the originating server are not copied with the documents in the collection.
Consider the following example:
This operation copies the
profiles
collection from theusers
database on the server atmongodb.example.net
. The operation only copies documents that satisfy the query{ active: true }
and does not copy indexes.cloneCollection
copies indexes by default, but you can disable this behavior by setting{ copyIndexes: false }
. Thequery
andcopyIndexes
arguments are optional.cloneCollection
creates a collection on the current database with the same name as the origin collection. If, in the above example, theprofiles
collection already exists in the local database, then MongoDB appends documents in the remote collection to the destination collection.