db.collection.copyTo()

On this page本页内容

Definition定义

db.collection.copyTo(newCollection)

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. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。

Deprecated since version 3.0.自3.0版以来已弃用。

Important重要的

Starting in version 4.2, MongoDB removes the eval command. 从4.2版开始,MongoDB删除了eval命令。The deprecated db.collection.copyTo(), which wraps the eval command, can only be run against MongoDB 4.0 or earlier versions. 不推荐使用的db.collection.copyTo()封装了eval命令,只能在MongoDB 4.0或更早版本上运行。For behavior and example, refer to the 4.0 or earlier version of the manual.有关行为和示例,请参阅4.0或更早版本的手册。

Copies all documents from collection into newCollection using server-side JavaScript. 使用服务器端JavaScript将所有文档从collection复制到newCollectionIf newCollection does not exist, MongoDB creates it.如果newCollection不存在,MongoDB将创建它。

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.如果复制失败,它将抛出异常。