@@ -7,68 +7,133 @@ findAndModify
7
7
.. dbcommand:: findAndModify
8
8
9
9
The :dbcommand:`findAndModify` command atomically modifies and
10
- returns a single document. The shell and many :term:`drivers
11
- <driver>` provide a :method:`findAndModify()` helper method. The
12
- command has the following prototype form:
10
+ returns a single document. By default, the returned document does
11
+ not include the modifications made on the update. To return the
12
+ document with the modifications made on the update, use the ``new``
13
+ option.
14
+
15
+ The command has the following syntax:
13
16
14
17
.. code-block:: javascript
15
18
16
- { findAndModify: "collection", <options> }
19
+ { findAndModify: <collection>, <options> }
20
+
21
+ The :dbcommand:`findAndModify` command takes the following are
22
+ sub-document ``options``:
23
+
24
+ :field document query:
25
+
26
+ Optional. Specifies the selection criteria for the
27
+ modification. The ``query`` field employs the same
28
+ :ref:`query selectors <query-selectors>` as used in the
29
+ :method:`db.collection.find()` method. Although the query may
30
+ match multiple documents, only one document will be selected
31
+ for the modification.
32
+
33
+ The ``query`` field has the following syntax:
34
+
35
+ .. code-block:: javascript
36
+
37
+ query: { <query expression> }
38
+
39
+ :field document sort:
40
+
41
+ Optional. Determines which document will be modified if the
42
+ query selects multiple documents. The first document as
43
+ determined by the ``sort`` will be the one modified.
44
+
45
+ The ``sort`` field has the following syntax:
46
+
47
+ .. code-block:: javascript
48
+
49
+ sort: { field1: value1, field2: value2, ... }
50
+
51
+ :field boolean remove:
52
+
53
+ Optional if ``update`` field exists. When ``true``, removes
54
+ the selected document. The default is ``false``.
55
+
56
+ The ``remove`` field has the following syntax:
57
+
58
+ .. code-block:: javascript
59
+
60
+ remove: <boolean>
17
61
18
- Replace, ``collection`` with the name of the collection
19
- containing the document that you want to modify, and specify
20
- options, as a sub-document that specifies the following:
62
+ :field document update:
21
63
22
- :field query: A query object. This statement might resemble the
23
- :term:`document` passed to :method:`db.collection.find()`, and
24
- should return *one* document from the database.
64
+ Optional if ``remove`` field exists. Performs an update of
65
+ the selected document. The ``update`` field employs the same
66
+ :ref:`update operators <update-operators>` or ``key:value``
67
+ specifications to modify the selected document.
25
68
26
- :field sort: Optional. If the query selects multiple documents, the
27
- first document given by this sort clause will be the
28
- one modified.
69
+ .. code-block:: javascript
29
70
30
- :field remove: When ``true``, :dbcommand:`findAndModify` removes
31
- the selected document.
71
+ update: { <update expression> }
32
72
33
- :field update: an :ref:`update operator <update-operators>` to
34
- modify the selected document.
73
+ :field boolean new:
35
74
36
- :field new: when ``true``, returns the modified document rather
37
- than the original. :dbcommand:`findAndModify` ignores
38
- the ``new`` option for ``remove`` operations.
75
+ Optional. When ``true``, returns the modified document rather
76
+ than the original. The :dbcommand:`findAndModify` method
77
+ ignores the ``new`` option for ``remove`` operations. The
78
+ default is ``false``.
39
79
40
- :field fields: a subset of fields to return. See ":ref:`projection
41
- operators <projection-operators>`" for more
42
- information.
80
+ .. code-block:: javascript
43
81
44
- :field upsert: when ``true``, creates a new document if the
45
- specified ``query`` returns no documents. The
46
- default is ``false``. The return value for these
47
- operations is ``null`` in version 2.2
82
+ new: <boolean>
48
83
49
- .. versionchanged:: 2.2
50
- Previously, ``upsert`` operations returned a an empty document
51
- (e.g. ``{ }``,) see :ref:`the 2.2 release notes
52
- <2.2-findandmodify-returns-null>` for more information.
84
+ :field document fields:
85
+
86
+ Optional. A subset of fields to return.
87
+
88
+ .. code-block:: javascript
53
89
54
- For example:
90
+ fields: { field1: <boolean>, field2: <boolean> ... }
91
+
92
+ :field boolean upsert:
93
+
94
+ Optional. Used in conjunction with the ``update`` field. When
95
+ ``true``, the :dbcommand:`findAndModify` command creates a
96
+ new document if the ``query`` returns no documents. The
97
+ default is ``false``. In version 2.2, the
98
+ :dbcommand:`findAndModify` command returns ``null`` when
99
+ ``upsert`` is ``true``.
100
+
101
+ .. code-block:: javascript
102
+
103
+ upsert: <boolean>
104
+
105
+ .. versionchanged:: 2.2
106
+ Previously, ``upsert`` operations returned a an empty
107
+ document (e.g. ``{ }``,) see :ref:`the 2.2 release notes
108
+ <2.2-findandmodify-returns-null>` for more information.
109
+
110
+ Consider the following example:
55
111
56
112
.. code-block:: javascript
57
113
58
114
{ findAndModify: "people",
59
- { query: { name: "Tom", state: "active", rating: { $gt: 10 } },
60
- sort: { rating: 1 },
61
- update: { $inc: { score: 1 } }
62
- }
63
- }
64
-
65
- This operation, finds a document in the ``people`` collection
66
- where the ``name`` field has the value ``Tom``, the
67
- ``active`` value in the ``state`` field and a value in the
68
- ``rating`` field :operator:`greater than <$gt>` 10. If there is
69
- more than one result for this query, MongoDB sorts the results of
70
- the query in descending order, and :operator:`increments <$inc>`
71
- the value of the ``score`` field by 1. Using the shell helper,
115
+ query: { name: "Tom", state: "active", rating: { $gt: 10 } },
116
+ sort: { rating: 1 },
117
+ update: { $inc: { score: 1 } }
118
+ }
119
+
120
+ This command performs the following actions:
121
+
122
+ #. The ``query`` finds a document in the ``people`` collection where the
123
+ ``name`` field has the value ``Tom``, the ``state`` field has the
124
+ value ``active`` and the ``rating`` field has a value
125
+ :operator:`greater than <$gt>` 10.
126
+
127
+ #. The ``sort`` orders the results of the query in ascending order.
128
+
129
+ #. The update :operator:`increments <$inc>` the value of the
130
+ ``score`` field by 1.
131
+
132
+ #. The command returns the original (pre-modification) document
133
+ selected for this update.
134
+
135
+ The shell and many :term:`drivers <driver>` provide a
136
+ :method:`findAndModify()` helper method. Using the shell helper,
72
137
this same operation can take the following form:
73
138
74
139
.. code-block:: javascript
0 commit comments