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

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

.. phpmethod:: MongoDB\Collection::replaceOne()

   Replace at most one document that matches the filter criteria. If multiple
   documents match the filter criteria, only the :term:`first <natural order>`
   matching document will be replaced.

   .. code-block:: php

      function replaceOne(
          array|object $filter,
          array|object $replacement,
          array $options = []
      ): MongoDB\UpdateResult

Parameters
----------

``$filter`` : array|object
  The filter criteria that specifies the documents to replace.

``$replacement`` : array|object
  The replacement document.

``$options`` : array
  An array specifying the desired options.

  .. list-table::
     :header-rows: 1
     :widths: 20 20 80

     * - Name
       - Type
       - Description

     * - bypassDocumentValidation
       - boolean
       - If ``true``, allows the write operation to circumvent document level
         validation. Defaults to ``false``.

     * - codec
       - MongoDB\\Codec\\DocumentCodec
       - .. include:: /includes/extracts/collection-option-codec.rst

         .. versionadded:: 1.17

     * - collation
       - array|object
       - .. include:: /includes/extracts/collection-option-collation.rst

     * - comment
       - mixed
       - .. include:: /includes/extracts/common-option-comment.rst

         .. include:: /includes/extracts/option-requires-4.4.rst

         .. versionadded:: 1.13

     * - hint
       - string|array|object
       - .. include:: /includes/extracts/common-option-hint.rst

         .. include:: /includes/extracts/option-requires-4.2.rst

         .. versionadded:: 1.6

     * - let
       - array|object
       - .. include:: /includes/extracts/common-option-let.rst

         .. versionadded:: 1.13

     * - session
       - :php:`MongoDB\Driver\Session <class.mongodb-driver-session>`
       - .. include:: /includes/extracts/common-option-session.rst

         .. versionadded:: 1.3

     * - upsert
       - boolean
       - If set to ``true``, creates a new document when no document matches the
         query criteria. The default value is ``false``, which does not insert a
         new document when no match is found.

     * - writeConcern
       - :php:`MongoDB\Driver\WriteConcern <class.mongodb-driver-writeconcern>`
       - .. include:: /includes/extracts/collection-option-writeConcern.rst

         .. include:: /includes/extracts/common-option-writeConcern-transaction.rst

Return Values
-------------

A :phpclass:`MongoDB\UpdateResult` object, which encapsulates a
:php:`MongoDB\Driver\WriteResult <class.mongodb-driver-writeresult>` object.

Errors/Exceptions
-----------------

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

Behavior
--------

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

Example
-------

The following example replaces the document with ``restaurant_id`` of
``"40356068"`` in the ``restaurants`` collection in the ``test`` database:

.. code-block:: php

   <?php

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

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

   printf("Matched %d document(s)\n", $updateResult->getMatchedCount());
   printf("Modified %d document(s)\n", $updateResult->getModifiedCount());

The output would then resemble:

.. code-block:: none

   Matched 1 document(s)
   Modified 1 document(s)

See Also
--------

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