db.collection.initializeUnorderedBulkOp()

On this page本页内容

Tip

Starting in version 3.2, MongoDB also provides the db.collection.bulkWrite() method for performing bulk write operations.从3.2版开始,MongoDB还提供了db.collection.bulkWrite()方法来执行批量写入操作。

Definition定义

db.collection.initializeUnorderedBulkOp()

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。

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批量执行这些操作。

Behavior行为

Order of Operation操作顺序

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()

Execution of Operations执行操作

When executing an unordered list of operations, MongoDB groups the operations. 在执行无序的操作列表时,MongoDB会对操作进行分组。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.因此,应用程序在执行无序批量操作时不应依赖于排序。

Each group of operations can have at most 1000 operations. 每组操作最多可以有1000个操作。If a group exceeds this limit, MongoDB will divide the group into smaller groups of 1000 or less. 如果一个组超过此限制,MongoDB将把该组分成1000个或更少的小组。For example, if the bulk operations list consists of 2000 insert operations, MongoDB creates 2 groups, each with 1000 operations.例如,如果批量操作列表包含2000个插入操作,MongoDB将创建2个组,每个组包含1000个操作。

The sizes and grouping mechanics are internal performance details and are subject to change in future versions.尺寸和分组机制是内部性能细节,在未来版本中可能会发生更改。

To see how the operations are grouped for a bulk operation execution, call Bulk.getOperations() after the execution.要查看批量操作执行时操作的分组方式,请在执行后调用Bulk.getOperations()

Error Handling错误处理

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将继续处理列表中的其余写操作。

Example示例

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();