Definition
Parameters
- $operations: array
- An array containing the write operations to perform. - MongoDB\Collection::bulkWrite()supports- MongoDB\Collection::deleteMany(),- MongoDB\Collection::deleteOne(),- MongoDB\Collection::insertOne(),- MongoDB\Collection::replaceOne(),- MongoDB\Collection::updateMany(), and- MongoDB\Collection::updateOne()operations in the following array structure:- [ - [ 'deleteMany' => [ $filter ] ], - [ 'deleteOne' => [ $filter ] ], - [ 'insertOne' => [ $document ] ], - [ 'replaceOne' => [ $filter, $replacement, $options ] ], - [ 'updateMany' => [ $filter, $update, $options ] ], - [ 'updateOne' => [ $filter, $update, $options ] ], - ] - Arguments correspond to the respective operation methods. However, the - writeConcernoption is specified as a top-level option to- MongoDB\Collection::bulkWrite()instead of each individual operation.
- $options: array
- An array specifying the desired options. NameTypeDescription- builderEncoder - MongoDB\Codec\Encoder - Encoder to use for query and aggregation builders. If not set, this option defaults to a new instance of the - MongoDB\Builder\BuilderEncoderclass.- New in version 1.21. - bypassDocumentValidation - boolean - If - true, allows the write operation to circumvent document level validation. Defaults to- false.- codec - MongoDB\Codec\DocumentCodec - The Encode Data with Type Codecs to use for encoding or decoding documents. This option is mutually exclusive with the - typeMapoption.- Defaults to the collection's codec. Inheritance for a default - codecoption takes precedence over that of the- typeMapoption.- Bulk writes use the codec for - insertOneand- replaceOneoperations.- New in version 1.17. - comment - mixed - Enables users to specify an arbitrary comment to help trace the operation through the database profiler, currentOp output, and logs. - This option is available since MongoDB 4.4 and will result in an exception at execution time if specified for an older server version. - New in version 1.13. - let - array|object - Map of parameter names and values. Values must be constant or closed expressions that do not reference document fields. Parameters can then be accessed as variables in an aggregate expression context (e.g. - $$var).- This is not supported for server versions prior to 5.0 and will result in an exception at execution time if used. - New in version 1.13. - ordered - boolean - If - true: When a single write fails, the operation will stop without performing the remaining writes and throw an exception.- If - false: When a single write fails, the operation will continue with the remaining writes, if any, and throw an exception.- The default is - true.- session - Client session to associate with the operation. - New in version 1.3. - 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 - writeConcernoption when starting the transaction.
Return Values
A MongoDB\BulkWriteResult object, which encapsulates a
MongoDB\Driver\WriteResult object.
Errors/Exceptions
MongoDB\Exception\UnsupportedException if options are used and
not supported by the selected server (e.g. collation, readConcern,
writeConcern).
MongoDB\Exception\InvalidArgumentException for errors related to
the parsing of parameters or options.
MongoDB\Driver\Exception\BulkWriteException for errors related to the write operation. You can inspect the value returned by getWriteResult() to determine the nature of the error.
MongoDB\Driver\Exception\RuntimeException for other errors at the extension level (e.g. connection errors).
Behavior
If a MongoDB\Driver\Exception\BulkWriteException is thrown, you can call getWriteResult() and inspect the returned MongoDB\Driver\WriteResult object 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 (e.g. replication took too long). Alternatively, a write operation may have failed outright (e.g. unique key violation).
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
- Collection Bulk Write section of the Bulk Write Operations guide