On this page本页内容
Tip
Starting in version 3.2, MongoDB also provides the 从3.2版开始,MongoDB还提供了db.collection.bulkWrite()
method for performing bulk write operations.db.collection.bulkWrite()
方法来执行批量写入操作。
db.collection.
initializeOrderedBulkOp
()¶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驱动程序文档。
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批量执行的写入操作的有序列表。
Bulk() |
With an ordered operations list, MongoDB executes the write operations in the list serially.使用有序操作列表,MongoDB连续执行列表中的写操作。
When executing an 在执行有序的操作列表时,MongoDB根据操作类型和连续性对操作进行分组;即,相同类型的连续操作被分组在一起。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. 例如,如果一个有序列表有两个插入操作,然后是一个更新操作,然后是另一个插入操作,MongoDB会将这些操作分为三个单独的组:第一组包含两个插入操作,第二组包含更新操作,第三组包含最后一个插入操作。This behavior is subject to change in future versions.这种行为可能会在将来的版本中发生更改。
Each group of operations can have at most 每组操作最多可以有1000个操作。1000 operations
. If a group exceeds this 如果一个组超过此限制,MongoDB将把该组分成1000个或更少的小组。limit
, MongoDB will divide the group into smaller groups of 1000 or less. For example, if the bulk operations list consists of 2000 insert operations, MongoDB creates 2 groups, each with 1000 operations.例如,如果批量操作列表包含2000个插入操作,MongoDB将创建2个组,每个组包含1000个操作。
The sizes and grouping mechanics are internal performance details and are subject to change in future versions.尺寸和分组机制是内部性能细节,在未来版本中可能会发生更改。
To see how the operations are grouped for a bulk operation execution, call 要查看批量操作执行时操作的分组方式,请在执行后调用Bulk.getOperations()
after the execution.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.
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将返回,而不处理列表中的任何剩余写入操作。