/
db.collection.updateOne.txt
495 lines (329 loc) · 13.1 KB
/
db.collection.updateOne.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
=========================
db.collection.updateOne()
=========================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.collection.updateOne(filter, update, options)
.. versionadded:: 3.2
Updates a single document within the collection based on the filter.
The :method:`~db.collection.updateOne()` method has the following form:
.. versionchanged:: 3.6
.. code-block:: javascript
db.collection.updateOne(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ]
}
)
The :method:`~db.collection.updateOne()` method takes the following
parameters:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``filter``
- document
- The selection criteria for the update. The same :ref:`query
selectors <query-selectors>` as in the :method:`find()
<db.collection.find()>` method are available.
Specify an empty document ``{ }`` to update the first document returned in
the collection.
* - ``update``
- document
- The modifications to apply.
Use :doc:`/reference/operator/update/` such as :update:`$set`,
:update:`$unset`, or :update:`$rename`.
Using the :ref:`update() <update-parameter>` pattern of
``field: value`` for the ``update`` parameter throws an error.
* - ``upsert``
- boolean
- .. include:: /includes/extracts/updateOne-behavior-method.rst
* - ``writeConcern``
- document
- Optional. A document expressing the :doc:`write concern
</reference/write-concern>`. Omit to use the default write concern.
.. include:: /includes/extracts/transactions-operations-write-concern.rst
* - ``collation``
- document
- Optional.
.. include:: /includes/extracts/collation-option.rst
* - ``arrayFilters``
- array
- Optional. An array of filter documents that determines which array elements to
modify for an update operation on an array field.
.. include:: /includes/extracts/arrayFilters-details.rst
For examples, see :ref:`updateOne-arrayFilters`.
.. versionadded:: 3.6
:returns:
A document containing:
- A boolean ``acknowledged`` as ``true`` if the operation ran with
:term:`write concern` or ``false`` if write concern was disabled
- ``matchedCount`` containing the number of matched documents
- ``modifiedCount`` containing the number of modified documents
- ``upsertedId`` containing the ``_id`` for the upserted document
Behavior
--------
:method:`~db.collection.updateOne()` updates the first matching document in
the collection that matches the ``filter``, using the ``update`` instructions
to apply modifications.
If ``upsert: true`` and no documents match the ``filter``,
:method:`~db.collection.updateOne()` creates a new
document based on the ``filter`` criteria and ``update`` modifications. See
:ref:`updateOne-example-update-with-upsert`.
.. _updateOne-capped-collection:
Capped Collection
~~~~~~~~~~~~~~~~~
.. include:: /includes/extracts/capped-collection-immutable-document-size-update.rst
.. TBD - need to test more thoroughly to pin down func.
.. Sharded Collections
.. ~~~~~~~~~~~~~~~~~~~
Explainability
~~~~~~~~~~~~~~
.. |write-method| replace:: :method:`~db.collection.updateOne()`
.. |old-write-method| replace:: :method:`~db.collection.update()`
.. include:: /includes/fact-bulkwrite-explainable.rst
Transactions
~~~~~~~~~~~~
.. include:: /includes/extracts/transactions-supported-operation.rst
If the operation results in an upsert, the collection must already exist.
.. include:: /includes/extracts/transactions-operations-write-concern.rst
.. include:: /includes/extracts/transactions-usage.rst
.. |operation| replace:: :method:`db.collection.updateOne()`
.. _updateOne-method-examples:
Examples
--------
.. _updateOne-example-update:
Update
~~~~~~
The ``restaurant`` collection contains the following documents:
.. code-block:: javascript
{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan" },
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 },
{ "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : 0 }
The following operation updates a single document where
``name: "Central Perk Cafe"`` with the ``violations`` field:
.. code-block:: javascript
try {
db.restaurant.updateOne(
{ "name" : "Central Perk Cafe" },
{ $set: { "violations" : 3 } }
);
} catch (e) {
print(e);
}
The operation returns:
.. code-block:: javascript
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
If no matches were found, the operation instead returns:
.. code-block:: javascript
{ "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0 }
Setting ``upsert: true`` would insert the document if no match was found. See
:ref:`updateOne-example-update-with-upsert`
.. _updateOne-example-update-with-upsert:
Update with Upsert
~~~~~~~~~~~~~~~~~~
The ``restaurant`` collection contains the following documents:
.. code-block:: javascript
{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan", "violations" : 3 },
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 },
{ "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : "0" }
The following operation attempts to update the document with
``name : "Pizza Rat's Pizzaria"``, while ``upsert: true`` :
.. code-block:: javascript
try {
db.restaurant.updateOne(
{ "name" : "Pizza Rat's Pizzaria" },
{ $set: {"_id" : 4, "violations" : 7, "borough" : "Manhattan" } },
{ upsert: true }
);
} catch (e) {
print(e);
}
Since ``upsert:true`` the document is ``inserted`` based on the ``filter`` and
``update`` criteria. The operation returns:
.. code-block:: javascript
{
"acknowledged" : true,
"matchedCount" : 0,
"modifiedCount" : 0,
"upsertedId" : 4
}
The collection now contains the following documents:
.. code-block:: javascript
{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan", "violations" : 3 },
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 },
{ "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : 4 },
{ "_id" : 4, "name" : "Pizza Rat's Pizzaria", "Borough" : "Manhattan", "violations" : 7 }
The ``name`` field was filled in using the ``filter`` criteria, while the
``update`` operators were used to create the rest of the document.
The following operation updates the first document with ``violations`` that
are greater than ``10``:
.. code-block:: javascript
try {
db.restaurant.updateOne(
{ "violations" : { $gt: 10} },
{ $set: { "Closed" : true } },
{ upsert: true }
);
} catch (e) {
print(e);
}
The operation returns:
.. code-block:: javascript
{
"acknowledged" : true,
"matchedCount" : 0,
"modifiedCount" : 0,
"upsertedId" : ObjectId("56310c3c0c5cbb6031cafaea")
}
The collection now contains the following documents:
.. code-block:: javascript
{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan", "violations" : 3 },
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 },
{ "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : 4 },
{ "_id" : 4, "name" : "Pizza Rat's Pizzaria", "Borough" : "Manhattan", "grade" : 7 }
{ "_id" : ObjectId("56310c3c0c5cbb6031cafaea"), "Closed" : true }
Since no documents matched the filter, and ``upsert`` was ``true``,
:method:`~db.collection.updateOne` inserted the document with a generated
``_id`` and the ``update`` criteria only.
.. _updateOne-example-update-with-write-concern:
Update with Write Concern
~~~~~~~~~~~~~~~~~~~~~~~~~
Given a three member replica set, the following operation specifies a
``w`` of ``majority``, ``wtimeout`` of ``100``:
.. code-block:: javascript
try {
db.restaurant.updateOne(
{ "name" : "Pizza Rat's Pizzaria" },
{ $inc: { "violations" : 3}, $set: { "Closed" : true } },
{ w: "majority", wtimeout: 100 }
);
} catch (e) {
print(e);
}
If the primary and at least one secondary acknowledge each write operation
within 100 milliseconds, it returns:
.. code-block:: javascript
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
If the acknowledgement takes longer than the ``wtimeout`` limit, the following
exception is thrown:
.. code-block:: javascript
WriteConcernError({
"code" : 64,
"errInfo" : {
"wtimeout" : true
},
"errmsg" : "waiting for replication timed out"
}) :
Specify Collation
~~~~~~~~~~~~~~~~~
.. include:: /includes/extracts/collation-versionadded.rst
A collection ``myColl`` has the following documents:
.. code-block:: javascript
{ _id: 1, category: "café", status: "A" }
{ _id: 2, category: "cafe", status: "a" }
{ _id: 3, category: "cafE", status: "a" }
The following operation includes the :ref:`collation <collation>`
option:
.. code-block:: javascript
db.myColl.updateOne(
{ category: "cafe" },
{ $set: { status: "Updated" } },
{ collation: { locale: "fr", strength: 1 } }
);
.. _updateOne-arrayFilters:
Specify ``arrayFilters`` for an Array Update Operations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. include:: /includes/extracts/arrayFilters-blurb.rst
Update Elements Match ``arrayFilters`` Criteria
```````````````````````````````````````````````
Create a collection ``students`` with the following documents:
.. code-block:: javascript
db.students.insert([
{ "_id" : 1, "grades" : [ 95, 92, 90 ] },
{ "_id" : 2, "grades" : [ 98, 100, 102 ] },
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }
])
To modify all elements that are greater than or equal to ``100`` in the
``grades`` array, use the filtered positional operator
:update:`$[\<identifier\>]` with the ``arrayFilters`` option in the
:method:`db.collection.updateOne` method:
.. code-block:: javascript
db.students.updateOne(
{ grades: { $gte: 100 } },
{ $set: { "grades.$[element]" : 100 } },
{ arrayFilters: [ { "element": { $gte: 100 } } ] }
)
The operation updates the ``grades`` field of a single document, and
after the operation, the collection has the following documents:
.. code-block:: javascript
:emphasize-lines: 2
{ "_id" : 1, "grades" : [ 95, 92, 90 ] }
{ "_id" : 2, "grades" : [ 98, 100, 100 ] }
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }
Update Specific Elements of an Array of Documents
`````````````````````````````````````````````````
Create a collection ``students2`` with the following documents:
.. code-block:: javascript
db.students2.insert([
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 90, "std" : 4 },
{ "grade" : 85, "mean" : 85, "std" : 6 }
]
},
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 85, "mean" : 85, "std" : 4 }
]
}
])
To modify the value of the ``mean`` field for all elements in the
``grades`` array where the grade is greater than or equal to ``85``,
use the filtered positional operator :update:`$[\<identifier\>]` with
the ``arrayFilters`` in the :method:`db.collection.updateOne` method:
.. code-block:: javascript
db.students2.updateOne(
{ },
{ $set: { "grades.$[elem].mean" : 100 } },
{ arrayFilters: [ { "elem.grade": { $gte: 85 } } ] }
)
The operation updates the array of a single document, and after the
operation, the collection has the following documents:
.. code-block:: javascript
:emphasize-lines: 5-6
{
"_id" : 1,
"grades" : [
{ "grade" : 80, "mean" : 75, "std" : 6 },
{ "grade" : 85, "mean" : 100, "std" : 4 },
{ "grade" : 85, "mean" : 100, "std" : 6 }
]
}
{
"_id" : 2,
"grades" : [
{ "grade" : 90, "mean" : 75, "std" : 6 },
{ "grade" : 87, "mean" : 90, "std" : 3 },
{ "grade" : 85, "mean" : 85, "std" : 4 }
]
}
.. seealso:: To update multiple documents, see
:method:`db.collection.updateMany()`.