@@ -6,7 +6,9 @@ db.collection.update()
6
6
7
7
.. method:: db.collection.update(query, update, [options])
8
8
9
- .. versionchanged:: 2.2
9
+ .. 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
12
11
13
The :method:`db.collection.update()` method provides the ability to
12
14
modify a document in a collection.
@@ -33,70 +35,35 @@ db.collection.update()
33
35
parameters:
34
36
35
37
:param document query:
36
-
38
+
37
39
A :term:`document` that specifies the selection criteria for
38
40
the update. The ``query`` parameter employs the same
39
41
:ref:`query selectors <query-selectors>` as used in the
40
42
:method:`db.collection.find()` method.
41
43
42
- Consider the following example:
43
-
44
- .. code-block:: javascript
45
-
46
- db.products.update( { item: "book", qty: { $gt: 5 } }, ... }
47
-
48
- This query excerpt will update the document(s) in the
49
- ``products`` collection with the ``item`` field value equal
50
- to ``book`` and the ``qty`` field value greater than ``5``.
51
-
52
44
:param document update:
53
-
54
- A :term:`document` that specifies the modifications to apply.
55
-
56
- - If the ``update`` parameter contains any :ref:`update
57
- operators <update-operators>` expressions such as the
58
- :operator:`$set` operator expression, then:
59
-
60
- - the ``update`` parameter must contain only :ref:`update
61
- operators <update-operators>` expressions.
62
-
63
- - the :method:`db.collection.update()` method updates only
64
- the corresponding fields in the document.
65
-
66
- Consider the following example:
67
-
68
- .. code-block:: javascript
69
-
70
- db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6 }, $inc: { y: 5} } )
71
-
72
- This query will update in the ``products`` collection a
73
- single document that matches the ``query`` criteria and set
74
- the value of the field ``x`` to ``6`` and increment the
75
- value of the field ``y`` by ``5``. All other fields of the
76
- modified document will remain the same.
77
45
78
- - If the ``update`` parameter consists only of ``field:
79
- value`` expressions,
46
+ A :term:`document` that specifies the modifications to apply.
80
47
81
- - the :method:`db.collection. update()` method updates the
82
- document to contain only the :term:`_id` field and the
83
- fields in the ``updates`` parameter.
48
+ **If** the `` update`` parameter contains any :ref:`update
49
+ operators <update-operators>` expressions such as the
50
+ :operator:`$set` operator expression, then:
84
51
85
- - the :method:`db.collection. update()` method updates
86
- cannot update multiple documents .
52
+ - the `` update`` parameter must contain only :ref:`update
53
+ operators < update-operators>` expressions .
87
54
88
- Consider the following example:
55
+ - the :method:`db.collection.update()` method updates only
56
+ the corresponding fields in the document.
89
57
90
- .. code-block:: javascript
58
+ **If** the ``update`` parameter consists only of ``field:
59
+ value`` expressions, then:
91
60
92
- db.products.update( { item: "book", qty: { $gt: 5 } }, { x: 6, y: 15 } )
61
+ - the :method:`db.collection.update()` method updates the
62
+ document to contain only the :term:`_id` field and the
63
+ fields in the ``updates`` parameter.
93
64
94
- This query will update in the ``products`` collection a
95
- single document that matches the ``query`` criteria and set
96
- the value of the field ``x`` to ``6`` and set the value of
97
- the field ``y`` to ``15``. *All other fields* of the
98
- modified document will be removed other than the
99
- :term:`_id` field.
65
+ - the :method:`db.collection.update()` method updates cannot
66
+ update multiple documents.
100
67
101
68
:param document options:
102
69
@@ -105,36 +72,6 @@ db.collection.update()
105
72
use the ``options`` parameter instead of the individual
106
73
``upsert`` and ``multi`` parameters.
107
74
108
- Consider the following example which specifies the ``multi`` option:
109
-
110
- .. code-block:: javascript
111
-
112
- db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } }, { multi: true } )
113
-
114
- This query will update **all** documents in the ``products``
115
- collection that matches the ``query`` criteria, setting the
116
- value of the field ``x`` to ``6`` and the value of the field
117
- ``y`` to ``15``. All other fields in the updated documents
118
- remain unchanged.
119
-
120
- Consider the following example which specifies the ``upsert`` option:
121
-
122
- .. code-block:: javascript
123
-
124
- db.products.update( { item: "magazine", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } }, { upsert: true } )
125
-
126
- This query will either:
127
-
128
- - update a single document in the ``products`` collection
129
- that matches the ``query`` criteria, setting the value of
130
- the field ``x`` to ``6`` and the value of the field ``y``
131
- to ``15``, *or*
132
-
133
- - if no matching document exists, insert a document in the
134
- ``products`` collection, with the field ``item`` set to
135
- ``book``, the field ``x`` set to ``25``, and the field
136
- ``y`` set to ``50``.
137
-
138
75
:param boolean upsert:
139
76
140
77
Optional. A boolean that specifies whether to perform
@@ -147,23 +84,6 @@ db.collection.update()
147
84
criteria, insert a new document with the fields and values of
148
85
the ``query`` and ``update``.
149
86
150
- Consider the following example:
151
-
152
- .. code-block:: javascript
153
-
154
- db.products.update( { item: "book", qty: { $gt: 100 } }, { $set: { x: 25, y: 50 } }, true )
155
-
156
- This query will either:
157
-
158
- - update an existing document that matches the ``query``
159
- criteria, setting the field ``x`` to ``25`` and the field
160
- ``y`` to ``50``, *or*
161
-
162
- - if no matching document exists, insert into the
163
- ``products`` collection a new document with the field
164
- ``item`` set to ``book``, the field ``x`` set to ``25``,
165
- and the field ``y`` set to ``50``.
166
-
167
87
Starting in version 2.2, you can use the ``options``
168
88
parameter instead of the ``upsert`` parameter.
169
89
@@ -184,16 +104,85 @@ db.collection.update()
184
104
When ``true``, the :method:`db.collection.update()` method
185
105
updates all documents that meet the ``query`` criteria.
186
106
187
- Consider the following example:
107
+ Starting in version 2.2, you can use the ``options``
108
+ parameter instead of the ``multi`` parameter.
188
109
189
- .. code-block:: javascript
110
+ Examples
111
+ ~~~~~~~~
190
112
191
- db.products.update( { item: "book", qty: { $gt: 100 } }, { $set: { x: 25, y: 50 } }, false, true )
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:
192
117
193
- This query will update **all** documents in the ``products``
194
- collection that matches the ``query`` criteria, setting the
195
- value of the field ``x`` to ``25`` and the value of the field
196
- ``y`` to ``50``.
118
+ .. code-block:: javascript
197
119
198
- Starting in version 2.2, you can use the ``options``
199
- parameter instead of the ``multi`` parameter.
120
+ db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6 }, $inc: { y: 5} } )
121
+
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.
126
+
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:
131
+
132
+ .. code-block:: javascript
133
+
134
+ db.products.update( { item: "book", qty: { $gt: 5 } }, { x: 6, y: 15 } )
135
+
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.
141
+
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
+
146
+ .. code-block:: javascript
147
+
148
+ db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } }, { multi: true } )
149
+
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.
154
+
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
159
+
160
+ db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } }, false, true )
161
+
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:
166
+
167
+ .. code-block:: javascript
168
+
169
+ db.products.update( { item: "magazine", qty: { $gt: 5 } }, { $set: { x: 25, y: 50 } }, { upsert: true } )
170
+
171
+ This query will either:
172
+
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*
176
+
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``.
180
+
181
+ You can also perform the same update by calling the
182
+ :method:`db.collection.update()` method with the ``upsert`` parameter:
183
+
184
+ .. code-block:: javascript
185
+
186
+ db.products.update( { item: "magazine", qty: { $gt: 5 } }, { $set: { x: 25, y: 50 } }, true )
187
+
188
+
0 commit comments