=================================
MongoDB\\Collection::updateMany()
=================================

.. default-domain:: mongodb

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

Definition
----------

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

   Update all documents that match the filter criteria.

   .. code-block:: php

      function updateMany(
          array|object $filter,
          array|object $update,
          array $options = []
      ): MongoDB\UpdateResult

Parameters
----------

``$filter`` : array|object
  The filter criteria that specifies the documents to update.

``$update`` : array|object
  Specifies the field and value combinations to update and any relevant update
  operators. ``$update`` uses MongoDB's :manual:`update operators </reference/operator/update>`.
  Starting with MongoDB 4.2, an `aggregation pipeline
  <https://mongodb.com/docs/master/reference/command/update/#update-with-an-aggregation-pipeline>`_
  can be passed as this parameter.

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

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

     * - Name
       - Type
       - Description

     * - arrayFilters
       - array
       - An array of filter documents that determines which array elements to
         modify for an update operation on an array field.

         .. versionadded:: 1.3

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

     * - collation
       - array|object
       - .. include:: /includes/extracts/collection-option-collation.rst

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

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

         .. versionadded:: 1.13

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

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

         .. versionadded:: 1.6

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

         .. versionadded:: 1.13

     * - session
       - :php:`MongoDB\Driver\Session <class.mongodb-driver-session>`
       - .. include:: /includes/extracts/common-option-session.rst

         .. versionadded:: 1.3

     * - upsert
       - boolean
       - If set to ``true``, creates a new document when no document matches the
         query criteria. The default value is ``false``, which does not insert a
         new document when no match is found.

     * - 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\UpdateResult` 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/note-bson-comparison.rst
.. include:: /includes/extracts/bulkwriteexception-result.rst

Examples
--------

The following example updates all of the documents with the ``borough`` of
``"Queens"`` by setting the ``active`` field to ``true``:

.. code-block:: php

   <?php

   $collection = (new MongoDB\Client)->test->restaurants;

   $updateResult = $collection->updateMany(
       [ 'borough' => 'Queens' ],
       [ '$set' => [ 'active' => true ]]
   );

   printf("Matched %d document(s)\n", $updateResult->getMatchedCount());
   printf("Modified %d document(s)\n", $updateResult->getModifiedCount());

The output would then resemble:

.. code-block:: none

   Matched 5656 document(s)
   Modified 5656 document(s)

See Also
--------

- :phpmethod:`MongoDB\Collection::replaceOne()`
- :phpmethod:`MongoDB\Collection::updateOne()`
- :phpmethod:`MongoDB\Collection::bulkWrite()`
- :doc:`/tutorial/crud`
- :manual:`update </reference/command/update>` command reference in the MongoDB
  manual
