On this page本页内容
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()
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
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() operations builder object.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.此行为可能会在未来版本中更改。
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.
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将返回而不处理列表中的任何剩余写操作。
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" } ).remove(); bulk.find( { status: "P" } ).update( { $set: { comment: "Pending" } } ); bulk.execute();