MongoDB\Client::bulkWrite()
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
MongoDB\Client::bulkWrite()
New in version 2.1
Definition
Perform multiple write operations across multiple namespaces.
function bulkWrite(
BulkWriteCommand|ClientBulkWrite $bulk,
array $options = []
): MongoDB\BulkWriteCommandResult
Parameters
$bulk : [MongoDB\ClientBulkWrite](https://www.mongodb.com/docs/php-library/current/reference/class/mongodbclientbulkwrite.md#mongodb-phpclass-phpclass.MongoDB-ClientBulkWrite or
Tip: Prefer ClientBulkWrite API
We recommend using the ClientBulkWrite builder class and methods to specify write operations in a bulk write command instead of using the BulkWriteCommand class. ClientBulkWrite provides a fluent API with methods similar to CRUD methods from the [MongoDB\Collection](https://www.mongodb.com/docs/php-library/current/reference/class/mongodbcollection.md#mongodb-phpclass-phpclass.MongoDB-Collection class.
Represents the assembled bulk write command or builder. [MongoDB\Client::bulkWrite()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbclient-bulkwrite.md#mongodb-phpmethod-phpmethod.MongoDB-Client--bulkWrite-- supports deleteMany(), deleteOne(), insertOne(), replaceOne(), updateMany(), and updateOne() operations.
$options : array
An array specifying the desired options.
| Name | Type | Description |
|---|---|---|
| session | MongoDB\Driver\Session | Client session to associate with the operation. |
| writeConcern | MongoDB\Driver\WriteConcern | Write concern to use for the operation. Defaults to the collection's write concern. It is not possible to specify a write concern for individual operations as part of a transaction. Instead, set the writeConcern option when starting the transaction. |
Return Values
A [MongoDB\BulkWriteCommandResult](https://www.mongodb.com/docs/php-library/current/reference/class/mongodbbulkwritecommandresult.md#mongodb-phpclass-phpclass.MongoDB-BulkWriteCommandResult object.
Errors/Exceptions
[MongoDB\Exception\UnsupportedException](https://www.mongodb.com/docs/php-library/current/reference/exception-classes.md#mongodb-phpclass-phpclass.MongoDB-Exception-UnsupportedException if options are used and not supported by the selected server (e.g. collation, readConcern, writeConcern).
[MongoDB\Exception\InvalidArgumentException](https://www.mongodb.com/docs/php-library/current/reference/exception-classes.md#mongodb-phpclass-phpclass.MongoDB-Exception-InvalidArgumentException for errors related to the parsing of parameters or options.
MongoDB\Driver\Exception\RuntimeException for other errors at the extension level (e.g. connection errors).
MongoDB\Driver\Exception\BulkWriteCommandException for errors related to the write operation. You can inspect the value returned by getWriteErrors() to determine the nature of the error.
Behavior
If a MongoDB\Driver\Exception\BulkWriteCommandException is thrown, you can call getWriteErrors() and inspect the information in the returned array to determine the nature of the error.
For example, a write operation may have been successfully applied to the primary server but failed to satisfy the write concern. Alternatively, a write operation may have failed outright, for example for violating the unique key constraint.
In the case of a bulk write, the result might indicate multiple successful write operations and/or errors. If the ordered option is true, some operations may have succeeded before the first error was encountered and the exception thrown. If the ordered option is false, multiple errors may have been encountered.
See Also
-
[Client Bulk Write](https://www.mongodb.com/docs/php-library/current/crud/bulk-write.md#std-label-php-client-bulk-write section of the Bulk Write Operations guide
-
[CRUD Operations](https://www.mongodb.com/docs/php-library/current/crud/insert.md#std-label-php-write-insert
-
[
MongoDB\Collection::deleteMany()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbcollection-deletemany.md#mongodb-phpmethod-phpmethod.MongoDB-Collection--deleteMany-- -
[
MongoDB\Collection::deleteOne()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbcollection-deleteone.md#mongodb-phpmethod-phpmethod.MongoDB-Collection--deleteOne-- -
[
MongoDB\Collection::insertMany()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbcollection-insertmany.md#mongodb-phpmethod-phpmethod.MongoDB-Collection--insertMany-- -
[
MongoDB\Collection::insertOne()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbcollection-insertone.md#mongodb-phpmethod-phpmethod.MongoDB-Collection--insertOne-- -
[
MongoDB\Collection::replaceOne()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbcollection-replaceone.md#mongodb-phpmethod-phpmethod.MongoDB-Collection--replaceOne-- -
[
MongoDB\Collection::updateMany()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbcollection-updatemany.md#mongodb-phpmethod-phpmethod.MongoDB-Collection--updateMany-- -
[
MongoDB\Collection::updateOne()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbcollection-updateone.md#mongodb-phpmethod-phpmethod.MongoDB-Collection--updateOne--
