MongoDB.local SF, Jan 15: See the speaker lineup & ship your AI vision faster. Use WEB50 to save 50%
Find out more >
Docs Menu
Docs Home
/ /
Collection Methods

db.collection.copyTo()

db.collection.copyTo(newCollection)

Important

mongo Shell Method

This page documents the mongo shell 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 eval command. The deprecated db.collection.copyTo(), which wraps the eval command, 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 collection into newCollection using server-side JavaScript. If newCollection does not exist, MongoDB creates it.

Parameter
Type
Description

newCollection

string

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.

Back

db.collection.bulkWrite()

On this page