=================================
MongoDB\\Collection::replaceOne()
=================================

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

              
Definition
----------

.. phpmethod:: MongoDB\\Collection::replaceOne
   
   Replaces at most one document that matches the filter.
   
   .. code-block:: php
                   
      function replaceOne($filter, $replacement, array $options = []): MongoDB\UpdateResult

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

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

Output
------

Returns a ``MongoDB\UpdateResult`` object.

Example
-------

The following operation replaces the document with ``restaurant_id``
``40356068`` in the ``restaurants`` collection in the ``example`` database:

.. code-block:: php
                
   <?php
   
   $database = new MongoDB\Client;

   $collection = $database->selectCollection('users','restaurants');

   $replacement = $collection->replaceOne(
       [   'restaurant_id' => '40356068'],
       [
           'name' => 'New Restaurant',
           'restaurant_id' => '99988877',
           'borough' => 'Queens',
           'cuisine' => 'Cafe',
           'grades' => [],
       ]
   );

   
   var_dump($replacement);

The output would resemble:

.. code-block:: none
                
   object(MongoDB\UpdateResult)#13 (2) {
     ["writeResult":"MongoDB\UpdateResult":private]=>
     object(MongoDB\Driver\WriteResult)#12 (9) {
       ["nInserted"]=>
       int(0)
       ["nMatched"]=>
       int(1)
       ["nModified"]=>
       int(1)
       ["nRemoved"]=>
       int(0)
       ["nUpserted"]=>
       int(0)
       ["upsertedIds"]=>
       array(0) {
       }
       ["writeErrors"]=>
       array(0) {
       }
       ["writeConcernError"]=>
       NULL
       ["writeConcern"]=>
       array(4) {
         ["w"]=>
         NULL
         ["wmajority"]=>
         bool(false)
         ["wtimeout"]=>
         int(0)
         ["journal"]=>
         NULL
       }
     }
     ["isAcknowledged":"MongoDB\UpdateResult":private]=>
     bool(true)
   }

.. seealso::
   
   - :phpmethod:`MongoDB\\Collection::bulkWrite`
   - :phpmethod:`MongoDB\\Collection::updateMany`
   - :phpmethod:`MongoDB\\Collection::updateOne`
   - :doc:`/tutorial/crud`
   - :manual:`update </reference/command/update>` command reference
     in the MongoDB manual
