Docs HomeMongoDB Manual

Bulk()

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()方法来执行批量写入操作。

Description描述

Bulk()

Bulk operations builder used to construct a list of write operations to perform in bulk for a single collection. 批量操作生成器,用于为单个集合构造要批量执行的写入操作列表。To instantiate the builder, use either the db.collection.initializeOrderedBulkOp() or the db.collection.initializeUnorderedBulkOp() method.要实例化生成器,请使用db.collection.initializeOrderedBulkOp()db.collection.initializeUnorderedBulkOp()方法。

Ordered and Unordered Bulk Operations有序和无序批量操作

The builder can construct the list of operations as ordered or unordered.构建器可以将操作列表构造为有序的无序的

Ordered Operations有序操作

With an ordered operations list, MongoDB executes the write operations in the list serially. 通过一个有序的操作列表,MongoDB可以串行执行列表中的写操作。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将返回,而不处理列表中任何剩余的写操作。

Use db.collection.initializeOrderedBulkOp() to create a builder for an ordered list of write commands.使用db.collection.initializeOrderedBulkOp()为写入命令的有序列表创建生成器。

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. 当执行一个有序的操作列表时,MongoDB会根据操作类型和连续性对操作进行分组;即,将相同类型的连续操作分组在一起。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.此行为可能在未来版本中发生更改。

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()

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.在分片集合上执行有序操作列表通常比执行无序操作列表慢,因为对于有序列表,每个操作都必须等待上一个操作完成。

Unordered Operations无序操作

With an unordered operations list, MongoDB can execute in parallel, as well as in a nondeterministic order, the write operations in the list. 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可以并行执行列表中的写入操作,也可以按不确定的顺序执行。如果在处理其中一个写操作的过程中发生错误,MongoDB将继续处理列表中剩余的写操作。

Use db.collection.initializeUnorderedBulkOp() to create a builder for an unordered list of write commands.使用db.collection.initializeUnorderedBulkOp()为写入命令的无序列表创建生成器。

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

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()

Transactions事务

Bulk() can be used inside multi-document transactions.可以在多文档事务中使用Bulk()

For Bulk.insert() operations, the collection must already exist.对于Bulk.insert()操作,集合必须已经存在。

For Bulk.find.upsert(), if the operation results in an upsert, the collection must already exist.对于Bulk.find.upsert(),如果操作导致upsert,则集合必须已经存在。

Do not explicitly set the write concern for the operation if run in a transaction. 如果在事务中运行,请不要显式设置操作的写入关注。To use write concern with transactions, see Transactions and Write Concern.要在事务中使用写入关注,请参阅事务和写入关注

Important

In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. 在大多数情况下,与单文档写入相比,多文档事务会产生更高的性能成本,并且多文档事务的可用性不应取代有效的模式设计。For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. 对于许多场景,非规范化数据模型(嵌入文档和数组)将继续是您的数据和用例的最佳选择。That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.也就是说,对于许多场景,对数据进行适当建模将最大限度地减少对多文档事务的需求。

For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.有关其他事务使用注意事项(如运行时限制和操作日志大小限制),请参阅生产注意事项

Methods方法

The Bulk() builder has the following methods:Bulk()生成器具有以下方法:

Name名称Description描述
Bulk.insert()Adds an insert operation to a list of operations.将插入操作添加到操作列表中。
Bulk.find()Specifies the query condition for an update or a remove operation.指定更新或删除操作的查询条件。
Bulk.find.delete()Adds a multiple document delete operation to a list of operations.将多文档删除操作添加到操作列表中。
Bulk.find.deleteOne()Adds a single document delete operation to a list of operations.将单个文档删除操作添加到操作列表中。
Bulk.find.remove()An alias for Bulk.find.delete().Bulk.find.delete()的别名。
Bulk.find.removeOne()An alias for Bulk.find.deleteOne().Bulk.find.deleteOne()的别名。
Bulk.find.replaceOne()Adds a single document replacement operation to a list of operations.将单个文档替换操作添加到操作列表中。
Bulk.find.updateOne()Adds a single document update operation to a list of operations.将单个文档更新操作添加到操作列表中。
Bulk.find.update()Adds a multi update operation to a list of operations.multi更新操作添加到操作列表中。
Bulk.find.upsert()Specifies upsert: true for an update operation.为更新操作指定upsert: true
Bulk.execute()Executes a list of operations in bulk.批量执行操作列表。
Bulk.getOperations()Returns an array of write operations executed in the Bulk() operations object.返回在Bulk()操作对象中执行的写入操作的数组。
Bulk.toJSON()Returns a JSON document that contains the number of operations and batches in the Bulk() operations object.返回一个JSON文档,该文档包含Bulk()操作对象中的操作数和批处理数。
Bulk.toString()Returns the Bulk.toJSON() results as a string.以字符串形式返回Bulk.toJSON()结果。