Skip to content

DOCS-4873 #2193

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 2 commits into from
Closed
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
86 changes: 63 additions & 23 deletions source/reference/command/findAndModify.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,6 @@ cluster. :dbcommand:`findAndModify` operations issued against
:program:`mongos` instances for non-sharded collections function
normally.

Concurrency
~~~~~~~~~~~

This command obtains a write lock on the affected database and
will block other operations until it has completed; however,
typically the write lock is short lived and equivalent to other
similar :method:`~db.collection.update()` operations.

.. _findAndModify-command-and-update:

Comparisons with the ``update`` Method
Expand Down Expand Up @@ -213,18 +205,20 @@ This command performs the following actions:

{
"lastErrorObject" : {
"connectionId" : 1,
"updatedExisting" : true,
"n" : 1,
"connectionId" : 1,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
},
"value" : {
"_id" : ObjectId("50f1d54e9beb36a0f45c6452"),
"name" : "Tom",
"state" : "active",
"rating" : 100,
"score" : 5
value" : {
"_id" : ObjectId("54f62d2885e4be1f982b9c9c"),
"name" : "Tom",
"state" : "active",
"rating" : 100,
"score" : 5
},
"ok" : 1
}
Expand Down Expand Up @@ -260,7 +254,7 @@ method returns only the unmodified document, or if ``new`` is
.. code-block:: javascript

{
"_id" : ObjectId("50f1d54e9beb36a0f45c6452"),
"_id" : ObjectId("54f62d2885e4be1f982b9c9c"),
"name" : "Tom",
"state" : "active",
"rating" : 100,
Expand Down Expand Up @@ -303,10 +297,12 @@ and returns a document with the following fields:

{
"lastErrorObject" : {
"connectionId" : 1,
"updatedExisting" : false,
"upserted" : ObjectId("50f2329d0092b46dae1dc98e"),
"upserted" : ObjectId("54f62c08c85d4472eadea26e"),
"n" : 1,
"connectionId" : 1,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
},
Expand All @@ -326,7 +322,7 @@ would contain ``null``:
"lastErrorObject" : {
"updatedExisting" : false,
"n" : 1,
"upserted" : ObjectId("5102f7540cb5c8be998c2e99")
"upserted" : ObjectId("54f62c8bc85d4472eadea26f")
},
"ok" : 1
}
Expand Down Expand Up @@ -362,19 +358,63 @@ The command returns the newly inserted document in the ``value`` field:

{
"lastErrorObject" : {
"connectionId" : 1,
"updatedExisting" : false,
"upserted" : ObjectId("50f47909444c11ac2448a5ce"),
"upserted" : ObjectId("54f62bbfc85d4472eadea26d"),
"n" : 1,
"connectionId" : 1,
"syncMillis" : 0,
"writtenTo" : null,
"err" : null,
"ok" : 1
},
"value" : {
"_id" : ObjectId("50f47909444c11ac2448a5ce"),
"_id" : ObjectId("54f62bbfc85d4472eadea26d"),
"name" : "Pascal",
"rating" : 25,
"score" : 1,
"state" : "active"
"score" : 1,
},
"ok" : 1
}

Sort and Remove
~~~~~~~~~~~~~~~

By including a ``sort`` specification on the ``rating`` field, the
following example removes from the ``people`` collection a single
document with the ``state`` value of ``active`` and the lowest
``rating`` among the matching documents:

.. code-block:: javascript

db.runCommand(
{
findAndModify: "people",
query: { state: "active" },
sort: { rating: 1 },
remove: true
}
)

The method returns the deleted document:

.. code-block:: sh

{
"lastErrorObject" : {
"connectionId" : 1,
"n" : 1,
"syncMillis" : ,
"writtenTo" : null,
"err" : null,
"ok" : 1
},
"value" : {
"_id" : ObjectId("54f62a6785e4be1f982b9c9b"),
"name" : "XYZ123",
"score" : 1,
"state" : "active",
"rating" : 3
},
"ok" : 1
}