Skip to content

DOCS-4968: findAndModify return document #2279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions source/includes/fact-findandmodify-return.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- the original (i.e. pre-modification) document if ``new`` is false, or

- the modified or inserted document if ``new: true``, or

- ``null`` if ``upsert`` is false and the query matches no documents during ``update`` or ``remove``.
57 changes: 3 additions & 54 deletions source/reference/command/findAndModify.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,40 +55,12 @@ The return document contains the following fields:
- The ``upserted`` field **only** appears if the ``update`` with the
``upsert: true`` operation results in an insertion.

- The ``value`` field that returns either:
- The ``value`` field that returns one of:

- the original (i.e. pre-modification) document if ``new`` is
false, or

- the modified or inserted document if ``new: true``.
.. include:: /includes/fact-findandmodify-return.rst

- The ``ok`` field that returns the status of the command.

.. note::

If the :dbcommand:`findAndModify` finds no matching document,
then:

- for ``update`` or ``remove`` operations, ``lastErrorObject`` does
not appear in the return document and the ``value`` field holds a
``null``.

.. code-block:: javascript

{ "value" : null, "ok" : 1 }

- for ``update`` with ``upsert: true`` operation that results in an
insertion, if the command also specifies ``new`` is ``false``
**and** specifies a ``sort``, the return document has a
``lastErrorObject``, ``value``, and ``ok`` fields, but the
``value`` field holds an empty document ``{}``.

- for ``update`` with ``upsert: true`` operation that results in an
insertion, if the command also specifies ``new`` is ``false`` but
does **not** specify a ``sort``, the return document has a
``lastErrorObject``, ``value``, and ``ok`` fields, but the
``value`` field holds a ``null``.

Behavior
--------

Expand Down Expand Up @@ -290,30 +262,7 @@ and returns a document with the following fields:
command, including the field ``upserted`` that contains the
``ObjectId`` of the newly inserted document, and

- The ``value`` field that contains an empty document ``{}`` as the
original document because the command included the ``sort`` option:

.. code-block:: sh

{
"lastErrorObject" : {
"connectionId" : 1,
"updatedExisting" : false,
"upserted" : ObjectId("54f62c08c85d4472eadea26e"),
"n" : 1,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
},
"value" : {

},
"ok" : 1
}

If the command did **not** include the ``sort`` option, the ``value`` field
would contain ``null``:
- The ``value`` field containing ``null``.

.. code-block:: sh

Expand Down
26 changes: 3 additions & 23 deletions source/reference/method/db.collection.findAndModify.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,9 @@ Definition
Return Data
-----------

The :method:`~db.collection.findAndModify()` method returns either:
the pre-modification document or, if ``new: true`` is set, the
modified document.
The :method:`~db.collection.findAndModify()` method returns one of:

.. note::

- If the query finds no document for ``update`` or ``remove``
operations, :method:`~db.collection.findAndModify()` returns
``null``.

- If the query finds no document for an ``update`` with an
``upsert`` operation, :method:`~db.collection.findAndModify()`
creates a new document. If ``new`` is ``false``, **and** the
``sort`` option is **NOT** specified, the method returns ``null``.

- If the query finds no document for an ``update`` with an
``upsert`` operation, :method:`~db.collection.findAndModify()`
creates a new document. If ``new`` is ``false``, **and** a
``sort`` option, the method returns an empty document ``{}``.
.. include:: /includes/fact-findandmodify-return.rst

Behavior
--------
Expand Down Expand Up @@ -181,11 +165,7 @@ This method performs the following actions:
the method.

If no document matched the ``query`` condition, the method
returns ``null``:

.. code-block:: javascript

null
returns ``null``.

Upsert
~~~~~~
Expand Down
8 changes: 8 additions & 0 deletions source/release-notes/3.0-compatibility.txt
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,14 @@ The minimum 3.0-compatible driver versions are:
General Compatibility Changes
-----------------------------

``findAndModify`` Return Document
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MongoDB 3.0 simplifies the behavior of the :dbcommand:`findAndModify` command.
Now the ``value`` field in the return document is always ``null`` if ``upsert``
is false and the query matched no documents during an ``update`` or
``remove`` operation.

``upsert:true`` with a Dotted ``_id`` Query
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down