Database Manual / Reference / mongosh Methods / Bulk Operations

db.collection.initializeUnorderedBulkOp() (mongosh method方法)

Tip

MongoDB also provides the Mongo.bulkWrite() method for performing bulk write operations.MongoDB还提供了Mongo.bulkWrite()方法来执行批量写入操作。

Definition定义

db.collection.initializeUnorderedBulkOp()

Important

mongosh Method方法

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.本页记录了一种mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

Initializes and returns a new Bulk() operations builder for a collection. The builder constructs an unordered list of write operations that MongoDB executes in bulk.初始化并返回集合的新Bulk()操作生成器。构建器构建了一个MongoDB批量执行的无序写操作列表。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

Behavior行为

Order of Operation运算顺序

With an unordered operations list, MongoDB can execute in parallel the write operations in the list and in any order. 使用无序操作列表,MongoDB可以以任何顺序并行执行列表中的写入操作。If the order of operations matter, use db.collection.initializeOrderedBulkOp() instead.如果操作顺序很重要,请改用db.collection.initializeOrderedBulkOp()

Execution of Operations操作执行

When executing an unordered list of operations, MongoDB groups the operations. With an unordered bulk operation, the operations in the list may be reordered to increase performance. 当执行unordered操作列表时,MongoDB会对操作进行分组。对于无序批量操作,列表中的操作可以重新排序以提高性能。As such, applications should not depend on the ordering when performing unordered bulk operations.因此,应用程序在执行unordered批量操作时不应依赖于顺序。

Bulk() operations in mongosh and comparable methods in the drivers do not have a limit for the number of operations in a group. mongosh中的Bulk()操作和驱动程序中的类似方法对组中的操作数量没有限制。To see how the operations are grouped for bulk operation execution, call Bulk.getOperations() after the execution.要查看如何为批量操作执行对操作进行分组,请在执行后调用Bulk.getOperations()

Error Handling错误处理

If an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list.如果在处理其中一个写入操作时发生错误,MongoDB将继续处理列表中的其余写入操作。

Example示例

The following initializes a Bulk() operations builder and adds a series of insert operations to add multiple documents:下面初始化Bulk()操作生成器,并添加一系列插入操作以添加多个文档:

var bulk = db.users.initializeUnorderedBulkOp();
bulk.insert( { user: "abc123", status: "A", points: 0 } );
bulk.insert( { user: "ijk123", status: "A", points: 0 } );
bulk.insert( { user: "mop123", status: "P", points: 0 } );
bulk.execute();