- Reference >
mongoShell Methods >- Collection Methods >
- db.collection.copyTo()
db.collection.copyTo()¶
On this page
Definition¶
-
db.collection.copyTo(newCollection)¶ mongoShell MethodThis page documents the
mongoshell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.Deprecated since version 3.0.
Important
Starting in version 4.2, MongoDB removes the
evalcommand. The deprecateddb.collection.copyTo(), which wraps theevalcommand, can only be run against MongoDB 4.0 or earlier versions. For behavior and example, refer to the 4.0 or earlier version of the manual.Copies all documents from
collectionintonewCollectionusing server-side JavaScript. IfnewCollectiondoes not exist, MongoDB creates it.Parameter Type Description newCollectionstring The name of the collection to write data to. db.collection.copyTo()returns the number of documents copied. If the copy fails, it throws an exception.