Skip to content

Commit 885d51a

Browse files
committed
DOCS-505 v2.2 has improved update() api
1 parent 931e4b9 commit 885d51a

File tree

1 file changed

+69
-65
lines changed

1 file changed

+69
-65
lines changed

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

Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ db.collection.update()
1414
modify a document in a collection.
1515

1616
The default behavior of the :method:`db.collection.update()` method
17-
is to update a single document. You can specify the ``multi`` option to
18-
update multiple documents that meet the ``query`` criteria.
19-
Additionally, you can specify the ``upsert`` option to have the
20-
:method:`db.collection.update()` method insert a document into the
21-
collection if no document meets the ``query`` criteria.
22-
23-
Starting in version 2.2, 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.
22+
23+
24+
In version 2.2, the :method:`db.collection.update()` method
2425
can take an ``options`` :term:`document` as a parameter to specify
2526
the ``multi`` and the ``upsert`` options. However, the
2627
:method:`db.collection.update()` still supports the ``upsert`` and
@@ -84,7 +85,7 @@ db.collection.update()
8485
criteria, insert a new document with the fields and values of
8586
the ``query`` and ``update``.
8687

87-
Starting in version 2.2, you can use the ``options``
88+
In version 2.2, you can use the ``options``
8889
parameter instead of the ``upsert`` parameter.
8990

9091
.. note::
@@ -104,85 +105,88 @@ db.collection.update()
104105
When ``true``, the :method:`db.collection.update()` method
105106
updates all documents that meet the ``query`` criteria.
106107

107-
Starting in version 2.2, you can use the ``options``
108+
In version 2.2, you can use the ``options``
108109
parameter instead of the ``multi`` parameter.
109110

110-
Examples
111-
~~~~~~~~
111+
Consider the following examples of the :method:`update()
112+
<db.collection.update()>` method:
112113

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

118-
.. code-block:: javascript
119+
.. code-block:: javascript
119120

120-
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6 }, $inc: { y: 5} } )
121+
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6 }, $inc: { y: 5} } )
121122

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

127-
- To replace completely all the fields in a single document with those
128-
in the ``update`` parameter, you can call the
129-
:method:`db.collection.update()` method with the ``update`` parameter
130-
consisting of ``key:value`` expressions, as in the following:
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:
131134

132-
.. code-block:: javascript
135+
.. code-block:: javascript
133136

134-
db.products.update( { item: "book", qty: { $gt: 5 } }, { x: 6, y: 15 } )
137+
db.products.update( { item: "book", qty: { $gt: 5 } }, { x: 6, y: 15 } )
135138

136-
This query will update in the ``products`` collection a single
137-
document that matches the ``query`` criteria and set the value of the
138-
field ``x`` to ``6`` and set the value of the field ``y`` to ``15``.
139-
All other fields of the modified document will be **removed** other
140-
than the :term:`_id` field.
139+
This query will update in the ``products`` collection a single
140+
document that matches the ``query`` criteria and set the value of
141+
the field ``x`` to ``6`` and set the value of the field ``y`` to
142+
``15``. All other fields of the modified document will be
143+
**removed** other than the :term:`_id` field.
141144

142-
- To update multiple documents, you can call the
143-
:method:`db.collection.update()` method with the ``options``
144-
parameter specifying the ``multi`` option, as in the following:
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:
145148

146-
.. code-block:: javascript
149+
.. code-block:: javascript
147150

148-
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } }, { multi: true } )
151+
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } }, { multi: true } )
149152

150-
This query will update **all** documents in the ``products``
151-
collection that matches the ``query`` criteria, setting the value of
152-
the field ``x`` to ``6`` and the value of the field ``y`` to ``15``.
153-
All other fields in the updated documents remain unchanged.
153+
This query will update **all** documents in the ``products``
154+
collection that matches the ``query`` criteria, setting the value
155+
of the field ``x`` to ``6`` and the value of the field ``y`` to
156+
``15``. All other fields in the updated documents remain unchanged.
154157

155-
You can also perform the same update by calling the
156-
:method:`db.collection.update()` method with the ``multi`` parameter:
157-
158-
.. code-block:: javascript
158+
You can also perform the same update by calling the
159+
:method:`db.collection.update()` method with the ``multi``
160+
parameter:
159161

160-
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } }, false, true )
162+
.. code-block:: javascript
161163

162-
- To update a single document or to insert a new document if no
163-
document matches the ``query`` criteria, you can call the
164-
:method:`db.collection.update()` method with the ``options``
165-
parameter specifying the ``upsert`` option, as in the following:
164+
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } }, false, true )
166165

167-
.. code-block:: javascript
168-
169-
db.products.update( { item: "magazine", qty: { $gt: 5 } }, { $set: { x: 25, y: 50 } }, { upsert: true } )
166+
- 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:
170170

171-
This query will either:
171+
.. code-block:: javascript
172+
173+
db.products.update( { item: "magazine", qty: { $gt: 5 } }, { $set: { x: 25, y: 50 } }, { upsert: true } )
172174

173-
- update a single document in the ``products`` collection that
174-
matches the ``query`` criteria, setting the value of the field
175-
``x`` to ``25`` and the value of the field ``y`` to ``50``, *or*
175+
This query will either:
176176

177-
- if no matching document exists, insert a document in the
178-
``products`` collection, with the field ``item`` set to ``magazine``,
179-
the field ``x`` set to ``25``, and the field ``y`` set to ``50``.
177+
- update a single document in the ``products`` collection that
178+
matches the ``query`` criteria, setting the value of the field
179+
``x`` to ``25`` and the value of the field ``y`` to ``50``, *or*
180180

181-
You can also perform the same update by calling the
182-
:method:`db.collection.update()` method with the ``upsert`` parameter:
181+
- if no matching document exists, insert a document in the
182+
``products`` collection, with the field ``item`` set to
183+
``magazine``, the field ``x`` set to ``25``, and the field ``y``
184+
set to ``50``.
183185

184-
.. code-block:: javascript
186+
You can also perform the same update by calling the
187+
:method:`db.collection.update()` method with the ``upsert``
188+
parameter:
185189

186-
db.products.update( { item: "magazine", qty: { $gt: 5 } }, { $set: { x: 25, y: 50 } }, true )
190+
.. code-block:: javascript
187191

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

0 commit comments

Comments
 (0)