=======================================
MongoDB\\Collection::findOneAndDelete()
=======================================

.. default-domain:: mongodb

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

Definition
----------

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

   Finds a single document matching the query and deletes it.

   .. code-block:: php

      function findOneAndDelete(
          array|object $filter = [],
          array $options = []
      ): object|null

Parameters
----------

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

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

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

     * - Name
       - Type
       - Description

     * - 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.4.rst

         .. versionadded:: 1.7

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

         .. versionadded:: 1.13

     * - maxTimeMS
       - integer
       - .. include:: /includes/extracts/common-option-maxTimeMS.rst

     * - projection
       - array|object
       - The :ref:`projection specification <projections>` to determine which
         fields to include in the returned documents. See
         :manual:`Project Fields to Return from Query </tutorial/project-fields-from-query-results>`
         and :manual:`Projection Operators </reference/operator/projection>` in
         the MongoDB manual.

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

         .. versionadded:: 1.3

     * - sort
       - array|object
       - The sort specification for the ordering of the results.

     * - typeMap
       - array
       - .. include:: /includes/extracts/collection-option-typeMap.rst

         This will be used for the returned result document.

     * - 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
-------------

An array or object for the document that was deleted, or ``null`` if no document
matched the query. The return type will depend on the ``typeMap`` option.

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

Behavior
--------

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

Examples
--------

The following example finds and deletes the document with ``restaurant_id`` of
``"40375376"`` from the ``restaurants`` collection in the ``test`` database:

.. code-block:: php

   <?php

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

   $deletedRestaurant = $collection->findOneAndDelete(
       [ 'restaurant_id' => '40375376' ],
       [
           'projection' => [
               'name' => 1,
               'borough' => 1,
               'restaurant_id' => 1,
           ],
       ]
   );

   var_dump($deletedRestaurant);

The output would then resemble:

.. code-block:: none

   object(MongoDB\Model\BSONDocument)#17 (1) {
     ["storage":"ArrayObject":private]=>
     array(4) {
       ["_id"]=>
       object(MongoDB\BSON\ObjectId)#11 (1) {
         ["oid"]=>
         string(24) "594d5ef280a846852a4b3f70"
       }
       ["borough"]=>
       string(9) "Manhattan"
       ["name"]=>
       string(15) "Agra Restaurant"
       ["restaurant_id"]=>
       string(8) "40375376"
     }
   }

See Also
--------

- :phpmethod:`MongoDB\Collection::findOneAndReplace()`
- :phpmethod:`MongoDB\Collection::findOneAndUpdate()`
- :manual:`findAndModify </reference/command/findAndModify>` command reference
  in the MongoDB manual
