Docs HomeMongoDB Manual

db.collection.initializeOrderedBulkOp()

Tip

Starting in version 3.2, MongoDB also provides the db.collection.bulkWrite() method for performing bulk write operations.从3.2版本开始,MongoDB还提供了db.collection.bulkWrite()方法来执行批量写入操作。

Definition定义

db.collection.initializeOrderedBulkOp()
Important

mongosh Method

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

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

Returns:返回值:new Bulk() operations builder object.新的Bulk()操作生成器对象。

Behavior行为

Order of Operation操作顺序

With an ordered operations list, MongoDB executes the write operations in the list serially.通过一个有序的操作列表,MongoDB可以串行执行列表中的写操作。

Execution of Operations运营的执行

When executing an ordered list of operations, MongoDB groups the operations by the operation type and contiguity; i.e. contiguous operations of the same type are grouped together. For example, if an ordered list has two insert operations followed by an update operation followed by another insert operation, MongoDB groups the operations into three separate groups: first group contains the two insert operations, second group contains the update operation, and the third group contains the last insert operation. This behavior is subject to change in future versions.当执行一个有序的操作列表时,MongoDB会根据操作类型连续性对操作进行分组;即,将相同类型的连续操作分组在一起。例如,如果一个有序列表有两个插入操作、一个更新操作和另一个插入操作,MongoDB会将这些操作分为三个单独的组:第一组包含两个插入运算,第二组包含更新运算,第三组包含最后一个插入运算。此行为可能在未来版本中发生更改。

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

Executing an ordered list of operations on a sharded collection will generally be slower than executing an unordered list since with an ordered list, each operation must wait for the previous operation to finish.在分片集合上执行有序操作列表通常比执行无序操作列表慢,因为对于有序列表,每个操作都必须等待上一个操作完成。

Error Handling错误处理

If an error occurs during the processing of one of the write operations, MongoDB will return without processing any remaining write operations in the list.如果在处理其中一个写操作的过程中发生错误,MongoDB将返回,而不处理列表中任何剩余的写操作。

Examples实例

The following initializes a Bulk() operations builder on the users collection, adds a series of write operations, and executes the operations:下面初始化users集合上的Bulk()操作生成器,添加一系列写操作,并执行这些操作:

var bulk = db.users.initializeOrderedBulkOp();
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.find( { status: "D" } ).delete();
bulk.find( { status: "P" } ).update( { $set: { comment: "Pending" } } );
bulk.execute();