Docs HomeMongoDB Manual

Bulk.getOperations()

Bulk.getOperations()

Returns an array of write operations executed through Bulk.execute(). 返回通过Bulk.execute()执行的写入操作的数组。The returned write operations are in groups as determined by MongoDB for execution. 返回的写操作按MongoDB确定的组进行执行。For information on how MongoDB groups the list of bulk write operations, see Bulk.execute() Behavior.有关MongoDB如何对批量写入操作列表进行分组的信息,请参阅Bulk.execute()行为

Only use Bulk.getOperations() after a Bulk.execute(). 仅在执行Bulk.execute()之后使用Bulk.getOperations()Calling Bulk.getOperations() before you call Bulk.execute() will result in an incomplete list.在调用Bulk.execute()之前调用Bulk.getOperations()将导致不完整的列表。

Example实例

The following initializes a Bulk() operations builder on the items collection, adds a series of write operations, executes the operations, and then calls getOperations() on the bulk builder object:下面初始化items集合上的Bulk()操作生成器,添加一系列写操作,执行这些操作,然后调用bulk生成器对象上的getOperations()

var bulk = db.items.initializeUnorderedBulkOp();

for (var i = 1; i <= 1500; i++) {
bulk.insert( { x: i } );
}

bulk.execute();
bulk.getOperations();

The getOperations() method returns an array with the operations executed. getOperations()方法返回一个数组,其中包含已执行的操作。The output shows that MongoDB divided the operations into 2 groups, one with 1000 operations and one with 500. For information on how MongoDB groups the list of bulk write operations, see Bulk.execute() Behavior输出显示MongoDB将操作分为两组,一组有1000个操作,另一组有500个操作。有关MongoDB如何对批量写入操作列表进行分组的信息,请参阅Bulk.execute()行为

Although the method returns all 1500 operations in the returned array, this page omits some of the results for brevity.尽管该方法返回返回的数组中的所有1500个操作,但为了简洁起见,本页省略了一些结果。

[
{
"originalZeroIndex" : 0,
"batchType" : 1,
"operations" : [
{ "_id" : ObjectId("53a8959f1990ca24d01c6165"), "x" : 1 },

... // Content omitted for brevity

{ "_id" : ObjectId("53a8959f1990ca24d01c654c"), "x" : 1000 }
]
},
{
"originalZeroIndex" : 1000,
"batchType" : 1,
"operations" : [
{ "_id" : ObjectId("53a8959f1990ca24d01c654d"), "x" : 1001 },

... // Content omitted for brevity

{ "_id" : ObjectId("53a8959f1990ca24d01c6740"), "x" : 1500 }
]
}
]

Returned Fields返回的字段

The array contains documents with the following fields:该数组包含具有以下字段的文档:

originalZeroIndex

Specifies the order in which the operation was added to the bulk operations builder, based on a zero index; e.g. first operation added to the bulk operations builder will have originalZeroIndex value of 0.指定基于零索引将操作添加到批量操作生成器的顺序;例如,添加到批量操作生成器的第一个操作的originalZeroIndex值为0

batchType

Specifies the write operations type.指定写入操作类型。

batchTypeOperation操作
1Insert
2Update
3Remove
operations

Array of documents that contain the details of the operation.包含操作详细信息的文档数组。

Tip

See also: 另请参阅: