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

.. 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::findOneAndReplace()

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

20
   .. code-block:: php
21

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

24
   This method has the following parameters:
25

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

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

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

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

35 36
An array object for either the original or the replaced 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
Consider the following document in the ``restaurants`` collection in the
57
``test`` database:
58 59

.. code-block:: javascript
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
   {
     "_id" : ObjectId("576023c7b02fa9281da4139e"),
     "address" : {
       "building" : "977",
       "coord" : [
         -74.06940569999999,
         40.6188443
       ],
       "street" : "Bay Street",
       "zipcode" : "10305"
     },
     "borough" : "Staten Island",
     "cuisine" : "French",
     "grades" : [
       {
         "date" : ISODate("2014-08-15T00:00:00Z"),
         "grade" : "A",
         "score" : 7
       },
       {
         "date" : ISODate("2014-02-13T00:00:00Z"),
         "grade" : "A",
         "score" : 5
       },
       {
         "date" : ISODate("2013-06-07T00:00:00Z"),
         "grade" : "A",
         "score" : 11
       }
     ],
     "name" : "Zest",
     "restaurant_id" : "41220906"
   }

95 96
The following operation replaces the document with ``restaurant_id`` of
``"41220906"`` with a new document:
97 98

.. code-block:: php
99

100 101
   <?php

102
   $collection = (new MongoDB\Client)->teset->restaurants;
103 104

   $replacedRestaurant = $collection->findOneAndReplace(
105
       [ 'restaurant_id' => '41220906' ],
106 107 108 109 110 111
       [
           'Borough' => 'Staten Island',
           'cuisine' => 'Italian',
           'grades' => [],
           'name' => 'Staten Island Pastaria',
           'restaurant_id' => '999999999',
112
       ],
113 114
       [ 'returnDocument' => MongoDB\Operation\FindOneAndReplace::RETURN_DOCUMENT_AFTER ]
   );
115

Jeremy Mikola's avatar
Jeremy Mikola committed
116
   var_dump($replacedRestaurant);
117 118

The output would then resemble::
119

120 121 122 123
   object(MongoDB\Model\BSONDocument)#18 (1) {
     ["storage":"ArrayObject":private]=>
     array(6) {
       ["_id"]=>
124
       object(MongoDB\BSON\ObjectId)#11 (1) {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
         ["oid"]=>
         string(24) "594d5ef380a846852a4b5837"
       }
       ["Borough"]=>
       string(13) "Staten Island"
       ["cuisine"]=>
       string(7) "Italian"
       ["grades"]=>
       object(MongoDB\Model\BSONArray)#17 (1) {
         ["storage":"ArrayObject":private]=>
         array(0) {
         }
       }
       ["name"]=>
       string(22) "Staten Island Pastaria"
       ["restaurant_id"]=>
       string(9) "999999999"
142 143 144
     }
   }

145 146
See Also
--------
147

148 149 150 151
- :phpmethod:`MongoDB\\Collection::findOneAndDelete()`
- :phpmethod:`MongoDB\\Collection::findOneAndUpdate()`
- :manual:`findAndModify </reference/command/findAndModify>` command reference
  in the MongoDB manual