Skip to content

Commit 4357ae4

Browse files
author
Sam Kleinman
committed
DOCS-505 edits for consistency and style
1 parent 9c862ac commit 4357ae4

File tree

1 file changed

+64
-62
lines changed

1 file changed

+64
-62
lines changed

source/reference/method/db.collection.update.txt

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,33 @@ db.collection.update()
77
.. method:: db.collection.update(query, update, [options])
88

99
.. versionchanged:: 2.2
10-
Added new interface to take an ``options`` :term:`document` as a
11-
parameter to specify the ``multi`` and the ``upsert`` options.
10+
The :program:`mongo` shell adds an updated interface that
11+
accepts an parameters in more clear :term:`document` form to
12+
specify ``multi`` and ``upsert`` options.
1213

13-
The :method:`db.collection.update()` method provides the ability to
14-
modify a document in a collection.
15-
16-
The default behavior of the :method:`db.collection.update()` method
17-
is to update a single document. However, you have the option to
18-
perform a ``multi`` update as well as an ``upsert`` update. A
19-
``multi`` update updates multiple documents that meet the query
20-
criteria. An :term:`upsert` update inserts a document if no document
21-
in the collection matches the ``query`` criteria.
14+
The :method:`update() <db.collection.update()>` method provides the ability to
15+
modify an existing document in a collection.
2216

17+
By default the :method:`update() <db.collection.update()>` method updates a
18+
single document. If you specify a ``multi`` update, the
19+
:method:`update() <db.collection.update()>` method will update all
20+
documents in the collection that match the query criteria. If you
21+
specify ``upsert``, the :method:`update() <db.collection.update()>`
22+
method will insert the document if no document matches the query
23+
criteria, and preform a conventional update otherwise. The
24+
operation has the following form:
2325

24-
In version 2.2, the :method:`db.collection.update()` method
25-
can take an ``options`` :term:`document` as a parameter to specify
26-
the ``multi`` and the ``upsert`` options. However, the
27-
:method:`db.collection.update()` still supports the ``upsert`` and
28-
``multi`` parameters to specify the ``multi`` and the ``upsert``
29-
options:
30-
3126
.. code-block:: javascript
3227

33-
db.collection.update(query, update, [upsert,] [multi])
34-
35-
The :method:`db.collection.update()` method takes the following
28+
db.collection.update(query, update, <upsert,> <multi>)
29+
30+
Before version 2.0, in the :program:`mongo` shell, ``upsert`` and
31+
``multi`` were positional boolean options. Since version 2.2, the
32+
:method:`update() <db.collection.update()>` method can *also*take an
33+
``options`` :term:`document` as a parameter to specify the
34+
``multi`` and the ``upsert`` options
35+
36+
The :method:`update() <db.collection.update()>` method takes the following
3637
parameters:
3738

3839
:param document query:
@@ -53,66 +54,67 @@ db.collection.update()
5354
- the ``update`` parameter must contain only :ref:`update
5455
operators <update-operators>` expressions.
5556

56-
- the :method:`db.collection.update()` method updates only
57+
- the :method:`update() <db.collection.update()>` method updates only
5758
the corresponding fields in the document.
5859

5960
**If** the ``update`` parameter consists only of ``field:
6061
value`` expressions, then:
6162

62-
- the :method:`db.collection.update()` method updates the
63+
- the :method:`update() <db.collection.update()>` method updates the
6364
document to contain only the :term:`_id` field and the
6465
fields in the ``updates`` parameter.
6566

66-
- the :method:`db.collection.update()` method updates cannot
67+
- the :method:`update() <db.collection.update()>` method updates cannot
6768
update multiple documents.
6869

69-
:param document options:
70+
:param document options: Optional.
7071

71-
Optional. A :term:`document` that specifies whether to
72+
A :term:`document` that specifies whether to
7273
perform an :term:`upsert` and/or a multiple update. You can
7374
use the ``options`` parameter instead of the individual
7475
``upsert`` and ``multi`` parameters.
7576

76-
:param boolean upsert:
77+
:param boolean upsert: Optional.
7778

78-
Optional. A boolean that specifies whether to perform
79+
A boolean that specifies whether to perform
7980
an :term:`upsert`.
8081

81-
When not specified, the default value is ``false``. When
82-
``true``, the :method:`db.collection.update()` method will
82+
The default value is ``false``. When
83+
``true``, the :method:`update() <db.collection.update()>` method will
8384
update an existing document that matches the ``query``
8485
selection criteria **or** if no document matches the
8586
criteria, insert a new document with the fields and values of
86-
the ``query`` and ``update``.
87+
the ``query`` and ``update`` arguments.
8788

88-
In version 2.2, you can use the ``options``
89-
parameter instead of the ``upsert`` parameter.
89+
In version 2.2 of the :program:`mongo` shell, you may also
90+
specify ``upsert`` in the ``options`` parameter.
9091

9192
.. note::
9293

9394
An upsert operation affects only *one* document, and
9495
cannot update multiple documents.
9596

96-
:param boolean multi:
97+
:param boolean multi: Optional.
9798

98-
Optional. A boolean that specifies whether to update multiple
99-
documents that meet the ``query`` criteria.
99+
A boolean that specifies whether to update multiple
100+
documents that meet the query criteria.
100101

101102
When not specified, the default value is ``false`` and the
102-
:method:`db.collection.update()` method updates a single
103+
:method:`update() <db.collection.update()>` method updates a single
103104
document that meet the ``query`` criteria.
104105

105-
When ``true``, the :method:`db.collection.update()` method
106+
When ``true``, the :method:`update() <db.collection.update()>` method
106107
updates all documents that meet the ``query`` criteria.
107108

108-
In version 2.2, you can use the ``options``
109-
parameter instead of the ``multi`` parameter.
109+
In version 2.2 of the :program:`mongo` shell, you may also
110+
specify ``multi`` in the ``options`` parameter.
110111

111112
Consider the following examples of the :method:`update()
112-
<db.collection.update()>` method:
113+
<db.collection.update()>` method. These examples all use the 2.2
114+
option to specify options in document form.
113115

114116
- To update specific fields in a single document, you can call the
115-
:method:`db.collection.update()` method with the ``update``
117+
:method:`update() <db.collection.update()>` method with the ``update``
116118
parameter consisting of :ref:`update operators <update-operators>`
117119
expressions, as in the following:
118120

@@ -121,52 +123,52 @@ db.collection.update()
121123
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6 }, $inc: { y: 5} } )
122124

123125
This query will update in the ``products`` collection a single
124-
document that matches the ``query`` criteria and set the value of
126+
document that matches the query criteria and set the value of
125127
the field ``x`` to ``6`` and increment the value of the field
126128
``y`` by ``5``. All other fields of the modified document will
127129
remain the same.
128130

129-
- To replace completely all the fields in a single document with
130-
those in the ``update`` parameter, you can call the
131-
:method:`db.collection.update()` method with the ``update``
132-
parameter consisting of ``key:value`` expressions, as in the
133-
following:
131+
- To replace all the fields in a single document with the fields in
132+
the ``update`` parameter, call the :method:`update()
133+
<db.collection.update()>` method with an ``update`` parameter
134+
that consists of ``key: value`` expressions, as in the following:
134135

135136
.. code-block:: javascript
136137

137138
db.products.update( { item: "book", qty: { $gt: 5 } }, { x: 6, y: 15 } )
138139

139140
This query will update in the ``products`` collection a single
140-
document that matches the ``query`` criteria and set the value of
141+
document that matches the query criteria and set the value of
141142
the field ``x`` to ``6`` and set the value of the field ``y`` to
142143
``15``. All other fields of the modified document will be
143144
**removed** other than the :term:`_id` field.
144145

145-
- To update multiple documents, you can call the
146-
:method:`db.collection.update()` method with the ``options``
147-
parameter specifying the ``multi`` option, as in the following:
146+
- To update multiple documents, call the
147+
:method:`update() <db.collection.update()>` method and specify the
148+
``multi`` option in the ``options`` argument, as in the following:
148149

149150
.. code-block:: javascript
150151

151152
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } }, { multi: true } )
152153

153-
This query will update **all** documents in the ``products``
154-
collection that matches the ``query`` criteria, setting the value
154+
This operation will update **all** documents in the ``products``
155+
collection that match the query criteria, setting the value
155156
of the field ``x`` to ``6`` and the value of the field ``y`` to
156157
``15``. All other fields in the updated documents remain unchanged.
157158

158-
You can also perform the same update by calling the
159-
:method:`db.collection.update()` method with the ``multi``
159+
You can perform the same operation by calling the
160+
:method:`update() <db.collection.update()>` method with the ``multi``
160161
parameter:
161162

162163
.. code-block:: javascript
163164

164165
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } }, false, true )
165166

166167
- To update a single document or to insert a new document if no
167-
document matches the ``query`` criteria, you can call the
168-
:method:`db.collection.update()` method with the ``options``
169-
parameter specifying the ``upsert`` option, as in the following:
168+
document matches the query criteria, you can call the
169+
:method:`update() <db.collection.update()>` and specify the
170+
``upsert`` option in the the ``options`` argument, as in the
171+
following:
170172

171173
.. code-block:: javascript
172174

@@ -175,7 +177,7 @@ db.collection.update()
175177
This query will either:
176178

177179
- update a single document in the ``products`` collection that
178-
matches the ``query`` criteria, setting the value of the field
180+
matches the query criteria, setting the value of the field
179181
``x`` to ``25`` and the value of the field ``y`` to ``50``, *or*
180182

181183
- if no matching document exists, insert a document in the
@@ -184,9 +186,9 @@ db.collection.update()
184186
set to ``50``.
185187

186188
You can also perform the same update by calling the
187-
:method:`db.collection.update()` method with the ``upsert``
189+
:method:`update() <db.collection.update()>` method with the ``upsert``
188190
parameter:
189191

190192
.. code-block:: javascript
191193

192-
db.products.update( { item: "magazine", qty: { $gt: 5 } }, { $set: { x: 25, y: 50 } }, true )
194+
db.products.update( { item: "magazine", qty: { $gt: 5 } }, { $set: { x: 25, y: 50 } }, { upsert: true } )

0 commit comments

Comments
 (0)