========================================
MongoDB\\Collection::findOneAndReplace()
========================================

.. default-domain:: mongodb
                    
.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

              
Definition
----------

.. phpmethod:: MongoDB\\Collection::findOneAndReplace
   
   Finds a single document matching the query and replaces it.
   
   .. code-block:: php
                   
      function findOneAndReplace($filter, $replacement, array $options = []): object|null

   ``findOneAndReplace()`` supports the following parameters:
   
   .. include:: /includes/apiargs/MongoDBCollection-method-findOneAndReplace-param.rst

   The ``$options`` parameter supports the following options:
                
   .. include:: /includes/apiargs/MongoDBCollection-method-findOneAndReplace-option.rst

   .. include:: /includes/extracts/bson-deserialization-findOneAndReplace.rst

Output
------

By default, returns the original document. To return the *new*
document, use the ``returnDocument`` option. If no document matched
the query, returns ``null``.

Examples
--------

Consider the following document in the ``restaurants`` collection in
the ``example`` database:

.. code-block:: javascript
                
   {
     "_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"
   }

The following operation replaces the document with ``restaurant_id :
41220906`` with the new specified document:

.. code-block:: php
                
   <?php
   
   $collection = $database->selectCollection('example','restaurants');

   $updateRestaurant = $collection->findOneAndReplace(
       [ 'restaurant_id' => '41220906' ],
       [ 'Borough' => 'Staten Island', 
         'cuisine' => 'Italian',
         'grades' => [],
         'name' => 'Staten Island Pastaria',
         'restaurant_id' => '999999999',
       ],
       [ 'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER ]
   );  

   

   var_dump($updateRestaurant)
      
The ``var_dump()`` output contains the new document, as in the following::
   
   object(stdClass)#14 (6) {
     ["_id"]=>
     object(MongoDB\BSON\ObjectID)#11 (1) {
       ["oid"]=>
       string(24) "576023c7b02fa9281da4139e"
     }
     ["borough"]=>
     string(13) "Staten Island"
     ["cuisine"]=>
     string(7) "Italian"
     ["grades"]=>
     array(0) {
     }
     ["name"]=>
     string(22) "Staten Island Pastaria"
     ["restaurant_id"]=>
     string(9) "999999999"
   }

.. seealso::
   
   - :phpmethod:`MongoDB\\Collection::findOneAndDelete`
   - :phpmethod:`MongoDB\\Collection::findOneAndUpdate`
   - :manual:`findAndModify </reference/command/findAndModify>`
     command reference in the MongoDB manual
