================================
MongoDB\\Collection::updateOne()
================================

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

              
Definition
----------

.. phpmethod:: MongoDB\\Collection::updateOne
   
   Finds a single document matching the query and updates it.
   
   .. code-block:: php
                   
      function updateOne($filter, $update, array $options = []): MongoDB\UpdateResult

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

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

Returns a ``MongoDB\UpdateResult`` object.

Examples
--------

The following operation updates the ``name`` of the restaurant with
``restaurant_id: 40356151`` to "Brunos on Astoria":

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

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

   $update = $collection->updateOne(
          [   'restaurant_id' => '40356151'],
          [
              '$set' => [
                   'name' => 'Brunos on Astoria'
              ]
          ]
      );

   

   var_dump($update);

The output would then resemble::
  
  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::replaceOne`
   - :phpmethod:`MongoDB\\Collection::updateMany`
   - :doc:`/tutorial/crud`
   - :manual:`update </reference/command/update>` command reference
     in the MongoDB manual.
