MongoDBCollection-updateMany.txt 2.18 KB
Newer Older
1 2 3 4 5
=================================
MongoDB\\Collection::updateMany()
=================================

.. default-domain:: mongodb
6

7 8 9 10 11 12 13 14 15
.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

16 17 18 19
.. phpmethod:: MongoDB\\Collection::updateMany()

   Update all documents that match the filter criteria.

20
   .. code-block:: php
21

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

24
   This method has the following parameters:
25

26 27 28
   .. include:: /includes/apiargs/MongoDBCollection-method-updateMany-param.rst

   The ``$options`` parameter supports the following options:
29

30 31
   .. include:: /includes/apiargs/MongoDBCollection-method-updateMany-option.rst

32 33
Return Values
-------------
34

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

38 39 40 41 42
Errors/Exceptions
-----------------

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

46 47 48 49
Behavior
--------

.. include:: /includes/extracts/note-bson-comparison.rst
50
.. include:: /includes/extracts/bulkwriteexception-result.rst
51

52 53 54
Examples
--------

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

.. code-block:: php

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

62 63 64 65
   $updateResult = $collection->updateMany(
       [ 'borough' => 'Queens' ],
       [ '$set' => [ 'active' => 'True' ]]
   );
66

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

The output would then resemble::
71 72 73 74

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

75 76 77 78 79 80 81 82 83
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