Tip
MongoDB also provides the MongoDB还提供了Mongo.bulkWrite() method for performing bulk write operations.Mongo.bulkWrite()方法来执行批量写入操作。
Definition定义
db.collection.initializeOrderedBulkOp()-
Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. 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 ordered list of write operations that MongoDB executes in bulk.Bulk()操作生成器。构建器构建MongoDB批量执行的写入操作的有序列表。Returns:返回new新的Bulk()operations builder object.Bulk()操作生成器对象。
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 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. 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. 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.ordered(有序)操作列表通常比执行unordered(无序)列表慢,因为使用有序列表,每个操作都必须等待前一个操作完成。
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();