- Reference >
mongo
Shell Methods >- Collection Methods >
- db.collection.copyTo()
db.collection.copyTo()¶
On this page
Definition¶
-
db.collection.
copyTo
(newCollection)¶ Deprecated since version 3.0.
Copies all documents from
collection
intonewCollection
using server-side JavaScript. IfnewCollection
does not exist, MongoDB creates it.If authorization is enabled, you must have access to all actions on all resources in order to run
db.collection.copyTo()
. Providing such access is not recommended, but if your organization requires a user to rundb.collection.copyTo()
, create a role that grantsanyAction
on anyResource. Do not assign this role to any other user.Parameter Type Description newCollection
string The name of the collection to write data to. Warning
When using
db.collection.copyTo()
check field types to ensure that the operation does not remove type information from documents during the translation from BSON to JSON.The
db.collection.copyTo()
method uses theeval
command internally. As a result, thedb.collection.copyTo()
operation takes a global lock that blocks all other read and write operations until thedb.collection.copyTo()
completes.
copyTo()
returns the number of documents
copied. If the copy fails, it throws an exception.
Behavior¶
Because copyTo()
uses
eval
internally, the copy operations will block all
other operations on the mongod
instance.
Example¶
The following operation copies all documents from the source
collection into the target
collection.
db.source.copyTo(target)