This version of the documentation is archived and no longer supported. To learn how to upgrade your version of PHP Library Manual, refer to the upgrade documentation.




Executes multiple write operations.

function bulkWrite(array $operations, array $options = []): MongoDB\BulkWriteResult

This method has the following parameters:

Parameter Type Description
$operations array

Optional. An array containing the write operations to perform. MongoDB\Collection::bulkWrite() supports deleteMany(), deleteOne(), insertOne(), replaceOne(), updateMany(), and 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 writeConcern option is specified as a top-level option to MongoDB\Collection::bulkWrite() instead of each individual operation.

$options array Optional. An array specifying the desired options.

The $options parameter supports the following options:

Option Type Description
bypassDocumentValidation boolean

Optional. If true, allows the write operation to circumvent document level validation. Defaults to false.

This option is available in MongoDB 3.2+ and is ignored for older server versions, which do not support document level validation.

ordered boolean

Optional. 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 MongoDB\Driver\Session

Optional. Client session to associate with the operation.

Sessions are not supported for server versions prior to 3.6.

New in version 1.3.

writeConcern MongoDB\Driver\WriteConcern

Optional. 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 with startTransaction.

Return Values

A MongoDB\BulkWriteResult object, which encapsulates a MongoDB\Driver\WriteResult object.


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. Users should inspect the value returned by getWriteResult() to determine the nature of the error.

MongoDB\Driver\Exception\RuntimeException for other errors at the driver level (e.g. connection errors).


If a MongoDB\Driver\Exception\BulkWriteException is thrown, users should 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 may 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.