Overview
In this guide, you can learn how to perform multiple write operations in a single database call by using bulk write operations.
Consider a scenario in which you want to insert a document into a collection, update multiple other documents, then delete a document. If you use individual methods, each operation requires its own database call. Instead, you can use a bulk operation to reduce the number of calls to the database.
Sample Data
The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To access this collection from your PHP application, instantiate a MongoDB\Client that connects to an Atlas cluster and assign the following value to your $collection variable:
$collection = $client->sample_restaurants->restaurants;
To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see Get Started with Atlas.
Bulk Operations
To run a bulk write operation, pass an array of write operations to the MongoDB\Collection::bulkWrite() method. Use the following syntax to specify the write operations:
[ [ 'deleteMany' => [ $filter ] ], [ 'deleteOne' => [ $filter ] ], [ 'insertOne' => [ $document ] ], [ 'replaceOne' => [ $filter, $replacement, $options ] ], [ 'updateMany' => [ $filter, $update, $options ] ], [ 'updateOne' => [ $filter, $update, $options ] ], ]
Tip
To learn more about delete, insert, replace, and update operations, see CRUD Operations.
When you call the bulkWrite() method, the library automatically runs the write operations in the order they're specified in the array. To learn how to instruct bulkWrite() to run the write operations in an arbitrary order, see Modify Bulk Write Behavior.
Example
This example runs the following write operations on the restaurants collection:
Insert operation to insert a document in which the
namevalue is'Mongo's Deli'Update operation to update the
cuisinefield of a document in which thenamevalue is'Mongo's Deli'Delete operation to delete all documents in which the
boroughvalue is'Manhattan'
$result = $collection->bulkWrite( [ [ 'insertOne' => [ ['name' => 'Mongo\'s Deli'], ['cuisine' => 'Sandwiches'], ['borough' => 'Manhattan'], ['restaurant_id' => '1234'], ], ], [ 'updateOne' => [ ['name' => 'Mongo\'s Deli'], ['$set' => ['cuisine' => 'Sandwiches and Salads']], ], ], [ 'deleteMany' => [ ['borough' => 'Manhattan'], ], ], ], );
Modify Bulk Write Behavior
You can modify the behavior of the MongoDB\Collection::bulkWrite() method by passing an array that specifies option values as a parameter. The following table describes the options you can set in the array:
Option | Description |
|---|---|
| Specifies whether the operation bypasses document validation. This lets you
modify documents that don't meet the schema validation requirements, if any
exist. For more information about schema validation, see Schema
Validation in the MongoDB Server
manual. |
| Sets the codec to use for encoding or decoding documents. Bulk writes
use the codec for |
| Sets the write concern for the operation. For more information, see Write Concern in the MongoDB Server manual. |
| Specifies a document with a list of values to improve operation readability. Values must be constant or closed expressions that don't reference document fields. For more information, see the let statement in the MongoDB Server manual. |
| If set to |
| Attaches a comment to the operation. For more information, see the insert command fields guide in the MongoDB Server manual. |
| Specifies the client session to associate with the operation. |
The following example calls the bulkWrite() method to perform an insert and delete operation and sets the ordered option to false:
$result = $collection->bulkWrite( [ [ 'insertOne' => [ ['name' => 'Mongo\'s Pizza'], ['cuisine' => 'Italian'], ['borough' => 'Queens'], ['restaurant_id' => '5678'], ], ], [ 'deleteOne' => [ ['restaurant_id' => '5678'], ], ], ], ['ordered' => false], );
If the library runs the insert operation first, one document is deleted. If it runs the delete operation first, no documents are deleted.
Note
Unordered bulk operations do not guarantee order of execution. The order can differ from the way you list them to optimize the runtime.
Return Value
The MongoDB\Collection::bulkWrite() method returns a MongoDB\BulkWriteResult object. This class contains the following member functions:
Function | Description |
|---|---|
| Returns the number of documents deleted, if any. |
| Returns the number of documents inserted, if any. |
| Returns a map of |
| Returns the number of documents matched during update and replace operations, if applicable. |
| Returns the number of documents modified, if any. |
| Returns the number of documents upserted, if any. |
| Returns a map of |
| Returns a boolean indicating whether the bulk operation was acknowledged. |
Additional Information
To learn how to perform individual write operations, see the following guides:
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: