对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

db. 集合.initializeOrderedBulkOp()(mongosh方法)

提示

MongoDB 还提供了 db.collection.bulkWrite() 方法用于执行批量写入操作。

db.collection.initializeOrderedBulkOp()

重要

mongosh 方法

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

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

为集合初始化并返回新的 Bulk() 操作构建器。该构建者可以构造 MongoDB 批量执行的写入操作的有序列表。

返回:

新的Bulk()操作构建器对象。

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

注意

所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

对于有序操作列表,MongoDB 会按顺序执行列表中的写入操作。

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.

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.

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.

如果在处理其中的一个写入操作期间出现错误,MongoDB 将返回而不处理列表中的任何其余写入操作。

Bulk()users以下示例在collection上初始化 操作构建器,添加一系列写入操作,并执行这些操作:

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();
给本页内容打分