================================
MongoDB\\Collection::bulkWrite()
================================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

.. phpmethod:: MongoDB\Collection::bulkWrite()

   Executes multiple write operations.

   .. code-block:: php

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

Parameters
----------

``$operations`` : array
  An array containing the write operations to perform.
  :phpmethod:`MongoDB\Collection::bulkWrite()` supports
  :phpmethod:`MongoDB\Collection::deleteMany()`,
  :phpmethod:`MongoDB\Collection::deleteOne()`,
  :phpmethod:`MongoDB\Collection::insertOne()`,
  :phpmethod:`MongoDB\Collection::replaceOne()`,
  :phpmethod:`MongoDB\Collection::updateMany()`, and
  :phpmethod:`MongoDB\Collection::updateOne()` operations in the
  following array structure:

  .. code-block:: php

     [
         [ '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
  :phpmethod:`MongoDB\Collection::bulkWrite()` instead of each individual
  operation.

``$options`` : array
  An array specifying the desired options.

  .. list-table::
     :header-rows: 1
     :widths: 20 20 80

     * - Name
       - Type
       - Description

     * - bypassDocumentValidation
       - boolean
       - If ``true``, allows the write operation to circumvent document level
         validation. Defaults to ``false``.

     * - codec
       - MongoDB\\Codec\\DocumentCodec
       - .. include:: /includes/extracts/collection-option-codec.rst

         Bulk writes use the codec for ``insertOne`` and ``replaceOne``
         operations.

         .. versionadded:: 1.17

     * - comment
       - mixed
       - .. include:: /includes/extracts/common-option-comment.rst

         .. include:: /includes/extracts/option-requires-4.4.rst

         .. versionadded:: 1.13

     * - let
       - array|object
       - .. include:: /includes/extracts/common-option-let.rst

         .. versionadded:: 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
       - :php:`MongoDB\Driver\Session <class.mongodb-driver-session>`
       - .. include:: /includes/extracts/common-option-session.rst

         .. versionadded:: 1.3

     * - writeConcern
       - :php:`MongoDB\Driver\WriteConcern <class.mongodb-driver-writeconcern>`
       - .. include:: /includes/extracts/collection-option-writeConcern.rst

         .. include:: /includes/extracts/common-option-writeConcern-transaction.rst

Return Values
-------------

A :phpclass:`MongoDB\BulkWriteResult` object, which encapsulates a
:php:`MongoDB\Driver\WriteResult <class.mongodb-driver-writeresult>` object.

Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-bulkwriteexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

Behavior
--------

.. include:: /includes/extracts/bulkwriteexception-result.rst
.. include:: /includes/extracts/bulkwriteexception-ordered.rst

.. todo: add output and examples

See Also
--------

- :phpmethod:`MongoDB\Collection::deleteMany()`
- :phpmethod:`MongoDB\Collection::deleteOne()`
- :phpmethod:`MongoDB\Collection::insertMany()`
- :phpmethod:`MongoDB\Collection::insertOne()`
- :phpmethod:`MongoDB\Collection::replaceOne()`
- :phpmethod:`MongoDB\Collection::updateMany()`
- :phpmethod:`MongoDB\Collection::updateOne()`
- :doc:`/tutorial/crud`
