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.initializeUnorderedBulkOp()
This is a 这是一种mongosh
method. mongosh
方法。This is not the documentation for 这不是Node.js
or other programming language specific driver methods.Node.js
或其他特定于编程语言的驱动程序方法的文档。
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 unordered list of write operations that MongoDB executes in bulk.构建器构建MongoDB批量执行的写操作的无序列表。
With an unordered operations list, MongoDB can execute in parallel the write operations in the list and in any order. 使用无序操作列表,MongoDB可以以任何顺序并行执行列表中的写入操作。If the order of operations matter, use 如果操作的顺序很重要,请改用db.collection.initializeOrderedBulkOp()
instead.db.collection.initializeOrderedBulkOp()
。
When executing an 当执行无序操作列表时,MongoDB会对操作进行分组。unordered
list of operations, MongoDB groups the operations. 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()
。
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将继续处理列表中的其余写操作。
The following initializes a 下面将初始化Bulk()
operations builder and adds a series of insert operations to add multiple documents:Bulk()
操作生成器,并添加一系列插入操作以添加多个文档:
var bulk = db.users.initializeUnorderedBulkOp(); 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.execute();