On this page本页内容
Tip
Starting in version 3.2, MongoDB also provides the db.collection.bulkWrite()
method for performing bulk write operations.
Bulk.
execute
()¶Executes the list of operations built by the Bulk()
operations builder.
Bulk.execute()
accepts the following parameter:
writeConcern |
document |
See Override Default Write Concern for an example. Do not explicitly set the write concern for the operation if run in a transaction. To use write concern with transactions, see Transactions and Write Concern. |
Returns: | A BulkWriteResult object that contains the status of the operation. |
---|
After execution, you cannot re-execute the Bulk()
object without reinitializing. See db.collection.initializeUnorderedBulkOp()
and db.collection.initializeOrderedBulkOp()
.
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.
Each group of operations can have at most 1000 operations
. If a group exceeds this limit
, MongoDB will divide the group into smaller groups of 1000 or less. For example, if the bulk operations list consists of 2000 insert operations, MongoDB creates 2 groups, each with 1000 operations.
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.
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.
When executing an 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.
Each group of operations can have at most 1000 operations
. If a group exceeds this limit
, MongoDB will divide the group into smaller groups of 1000 or less. For example, if the bulk operations list consists of 2000 insert operations, MongoDB creates 2 groups, each with 1000 operations.
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()
can be used inside multi-document transactions.
For Bulk.find.insert()
operations, the collection must already exist.
For Bulk.find.upsert()
, if the operation results in an upsert, the collection must already exist.
Do not explicitly set the write concern for the operation if run in a transaction. To use write concern with transactions, see Transactions and Write Concern.
Important
In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.
The following initializes a Bulk()
operations builder on the items
collection, adds a series of insert operations, and executes the operations:
The operation returns the following BulkWriteResult()
object:
For details on the return object, see BulkWriteResult()
. For details on the batches executed, see Bulk.getOperations()
.
The following operation to a replica set specifies a write concern of "w: majority"
with a wtimeout
of 5000 milliseconds such that the method returns after the writes propagate to a majority of the voting replica set members or the method times out after 5 seconds.
The operation returns the following BulkWriteResult()
object:
See
Bulk()
for a listing of methods available for bulk operations.