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

.. 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
.. phpmethod:: MongoDB\\Collection::findOneAndUpdate()

18
   Finds a single document matching the query and updates it.
19

20
   .. code-block:: php
21

22 23
      function findOneAndUpdate($filter, $update, array $options = []): object|null

24
   This method has the following parameters:
25

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

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

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

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

35 36
An array or object for either the original or the updated document, depending on
the specified value of the ``returnDocument`` option. By default, the original
37
document is returned. If no document matched the query, ``null`` is returned.
38
The return type will depend on the ``typeMap`` option.
39

40 41 42 43 44 45 46 47
Errors/Exceptions
-----------------

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

48 49 50 51 52
Behavior
--------

.. include:: /includes/extracts/note-bson-comparison.rst

53 54 55
Examples
--------

56
The following operation updates the restaurant with ``restaurant_id`` of
57
``"40361708"`` in the ``restaurants`` collection in the ``test`` database by
58
setting its building number to ``"761"``:
59 60 61

.. code-block:: php

62 63
   <?php

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

66
   $updatedRestaurant = $collection->findOneAndUpdate(
67
       [ 'restaurant_id' => '40361708' ],
68 69 70
       [ '$set' => [ 'address.building' => '761' ]],
       [
           'projection' => [ 'address' => 1 ],
71
           'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
72
       ]
73 74
   );

Jeremy Mikola's avatar
Jeremy Mikola committed
75
   var_dump($updatedRestaurant);
76

77 78
The output would then resemble::

79 80 81 82
   object(MongoDB\Model\BSONDocument)#20 (1) {
     ["storage":"ArrayObject":private]=>
     array(2) {
       ["_id"]=>
83
       object(MongoDB\BSON\ObjectId)#12 (1) {
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
         ["oid"]=>
         string(24) "594d5ef280a846852a4b3dee"
       }
       ["address"]=>
       object(MongoDB\Model\BSONDocument)#19 (1) {
         ["storage":"ArrayObject":private]=>
         array(4) {
           ["building"]=>
           string(3) "761"
           ["coord"]=>
           object(MongoDB\Model\BSONArray)#18 (1) {
             ["storage":"ArrayObject":private]=>
             array(2) {
               [0]=>
               float(-73.9925306)
               [1]=>
               float(40.7309346)
             }
           }
           ["street"]=>
           string(8) "Broadway"
           ["zipcode"]=>
           string(5) "10003"
         }
108 109 110
       }
     }
   }
111

112 113
See Also
--------
114

115 116 117 118
- :phpmethod:`MongoDB\\Collection::findOneAndDelete()`
- :phpmethod:`MongoDB\\Collection::findOneAndReplace()`
- :manual:`findAndModify </reference/command/findAndModify>` command reference
  in the MongoDB manual