Tip
MongoDB also provides MongoDB还提供db.collection.bulkWrite() for performing bulk write operations.db.collection.bulkWrite()来执行批量写入操作。
Description描述
Bulk.execute()Executes the list of operations built by the执行由Bulk()operations builder.Bulk()操作生成器构建的操作列表。Bulk.execute()accepts the following parameter:接受以下参数:Parameter参数Type类型Description描述writeConcerndocument文档Optional.可选。write concern document for the bulk operation as a whole. Omit to use default. For a standalone为整个批量操作写入关注文档。省略使用默认值。对于独立的mongodserver, the write concern defaults to{ w: majority }.mongod服务器,写入关注默认为{ w: majority }。With a replica set, the default write concern is对于副本集,默认的写入关注是{ w: majority }unless modified as part of the replica set configuration, or potentially if the replica set contains multiple arbiters.{ w: majority },除非作为副本集配置的一部分进行了修改,或者如果副本集包含多个仲裁器,则可能会进行修改。See Override Default Write Concern for an example.有关示例,请参阅覆盖默认写入关注。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.如果在事务中运行,则不要显式设置操作的写入关注。要对事务使用写关注,请参阅事务和写关注。Returns:返回A一个包含操作状态的BulkWriteResult()object that contains the status of the operation.BulkWriteResult()对象。After execution, you cannot re-execute the执行后,如果不重新初始化,就无法重新执行Bulk()object without reinitializing.Bulk()对象。See请参阅db.collection.initializeUnorderedBulkOp()anddb.collection.initializeOrderedBulkOp().db.collection.initializeUnorderedBulkOp()和db.collection.initializeOrderedBulkOp()。
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行为
Ordered 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. ordered操作列表时,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. This behavior is subject to change in future versions.例如,如果一个有序列表有两个插入操作、一个更新操作和另一个插入操作,MongoDB会将这些操作分为三个单独的组:第一组包含两个插入运算,第二组包含更新运算,第三组包含最后一个插入运算。此行为在未来的版本中可能会发生变化。
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.ordered操作列表通常比执行unordered列表慢,因为使用有序列表,每个操作都必须等待前一个操作完成。
Unordered 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()。
Transactions事务
Bulk() can be used inside distributed transactions.可以在分布式事务中使用。
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(),如果操作导致异常,则集合必须已经存在。
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, a distributed transaction incurs a greater performance cost over single document writes, and the availability of distributed 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 distributed transactions.对于许多场景,非规范化数据模型(嵌入式文档和数组)将继续是数据和用例的最佳选择。也就是说,对于许多场景,适当地对数据进行建模将最大限度地减少对分布式事务的需求。
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.有关其他事务使用注意事项(如运行时限制和oplog大小限制),另请参阅生产注意事项。
Examples示例
Execute Bulk Operations执行批量操作
The following initializes a 下面初始化Bulk() operations builder on the items collection, adds a series of insert operations, and executes the operations:items集合上的Bulk()操作生成器,添加一系列插入操作,并执行这些操作:
var bulk = db.items.initializeUnorderedBulkOp();
bulk.insert( { item: "abc123", status: "A", defaultQty: 500, points: 5 } );
bulk.insert( { item: "ijk123", status: "A", defaultQty: 100, points: 10 } );
bulk.execute( );
The operation returns the following 该操作返回以下BulkWriteResult() object:BulkWriteResult()对象:
BulkWriteResult({
acknowledged: true,
insertedCount: 2,
insertedIds: {
'0': ObjectId("64e61e3b84ff8808cd43a92c"),
'1': ObjectId("64e61e3b84ff8808cd43a92d")
},
matchedCount: 0,
modifiedCount: 0,
deletedCount: 0,
upsertedCount: 0,
upsertedIds: {}
})
For details on the return object, see 有关返回对象的详细信息,请参阅BulkWriteResult(). For details on the batches executed, see Bulk.getOperations().BulkWriteResult()。有关执行的批处理的详细信息,请参阅Bulk.getOperations()。
Override Default Write Concern覆盖默认写入关注
The following operation to a replica set specifies a write concern of 对副本集的以下操作指定了一个写入关注"w: 1" with a wtimeout of 5000 milliseconds such that the method returns after the writes propagate to a majority of the voting replica set members or the method times out after five seconds."w: 1",其wtimeout为5000毫秒,这样该方法在写入传播到大多数投票副本集成员后返回,或者该方法在五秒后超时。
var bulk = db.items.initializeUnorderedBulkOp();
bulk.insert( { item: "efg123", status: "A", defaultQty: 100, points: 0 } );
bulk.insert( { item: "xyz123", status: "A", defaultQty: 100, points: 0 } );
bulk.execute( { w: 1, wtimeout: 5000 } );
The operation returns the following 该操作返回以下BulkWriteResult() object:BulkWriteResult()对象:
BulkWriteResult({
acknowledged: true,
insertedCount: 2,
insertedIds: {
'0': ObjectId("64e61e3b84ff8808cd43a92c"),
'1': ObjectId("64e61e3b84ff8808cd43a92d")
},
matchedCount: 0,
modifiedCount: 0,
deletedCount: 0,
upsertedCount: 0,
upsertedIds: {}
})