@@ -29,13 +29,11 @@ Update Documents
29
29
Overview
30
30
--------
31
31
32
- In this guide, you can learn how to modify documents in MongoDB using
33
- **update** and **replace** operations.
32
+ In this guide, you can learn how to update documents in MongoDB using
33
+ **update** operations.
34
34
35
35
Update operations change the fields that you specify while leaving other
36
- fields and values unchanged. Replace operations remove all existing fields
37
- except for ``_id`` in a document and substitute the deleted fields with
38
- the new fields and values you specify.
36
+ fields and values unchanged.
39
37
40
38
In MongoDB, all methods to modify documents follow the same pattern:
41
39
@@ -52,23 +50,19 @@ The pattern expects you to:
52
50
* Specify the field and value changes.
53
51
* Specify options, if you must modify the method behavior.
54
52
55
- The driver provides the following methods to modify documents:
53
+ The driver provides the following methods to update documents:
56
54
57
55
* ``UpdateByID()``
58
56
* ``UpdateOne()``
59
57
* ``UpdateMany()``
60
- * ``ReplaceOne()``
61
- * ``BulkWrite()`` *(not discussed in this guide)*
62
- * ``FindOneAndUpdate()`` *(not discussed in this guide)*
63
- * ``FindOneAndReplace()`` *(not discussed in this guide)*
64
58
65
59
A Note About ``_id``
66
60
~~~~~~~~~~~~~~~~~~~~
67
61
68
62
Each document in a MongoDB collection has a unique and immutable ``_id``
69
- field. You cannot use update and replace operations to change the
70
- ``_id`` field. If you attempt to change this field, the update and
71
- replace methods return a ``WriteError``.
63
+ field. You cannot use update operations to change the
64
+ ``_id`` field. If you attempt to change this field, the update
65
+ methods return a ``WriteError``.
72
66
73
67
.. _golang-update-documents:
74
68
@@ -151,7 +145,7 @@ See our :ref:`upsert guide <golang-upsert-guide>`
151
145
to learn how to insert a new document if no documents match the query filter.
152
146
153
147
UpdateOne() Example
154
- -------------------
148
+ ~~~~~~~~~~~~~~~~~~~
155
149
156
150
The following document describes an employee:
157
151
@@ -210,7 +204,7 @@ The following shows the updated document resulting from the preceding update ope
210
204
}
211
205
212
206
UpdateMany() Example
213
- --------------------
207
+ ~~~~~~~~~~~~~~~~~~~~
214
208
215
209
The following example uses the ``listingsAndReviews`` collection in the
216
210
``sample_airbnb`` dataset from the :atlas:`Atlas sample datasets </sample-data>`.
@@ -242,121 +236,14 @@ documents in the ``listingsAndReviews`` collection:
242
236
243
237
For an example on how to find multiple documents, see :ref:`golang-find-example`.
244
238
245
- .. _golang-replacement-document:
246
-
247
- Replace
248
- -------
249
-
250
- Use the ``ReplaceOne()`` method to replace a single document.
251
-
252
- Parameters
253
- ~~~~~~~~~~
254
-
255
- ``ReplaceOne()`` expects a **replacement document**, which is the document
256
- that you want to take the place of an existing document. Replacement
257
- documents use the following format:
258
-
259
- .. code-block:: go
260
-
261
- bson.D{{"<field>", "<value>"}, {"<field>", "<value>"}, ... }
262
-
263
- Return Values
264
- ~~~~~~~~~~~~~
265
-
266
- ``ReplaceOne()`` returns an ``UpdateResult`` type that
267
- contains information about the replace operation if the operation is
268
- successful. The ``UpdateResult`` type contains the following properties:
269
-
270
- .. list-table::
271
- :widths: 30 70
272
- :header-rows: 1
273
-
274
- * - Property
275
- - Description
276
-
277
- * - ``MatchedCount``
278
- - The number of documents matched by the filter
279
-
280
- * - ``ModifiedCount``
281
- - The number of documents modified by the operation
282
-
283
- * - ``UpsertedCount``
284
- - The number of documents upserted by the operation
285
-
286
- * - ``UpsertedID``
287
- - The ``_id`` of the upserted document, or ``nil`` if there is none
288
-
289
- If multiple documents match the query filter passed to ``ReplaceOne()``,
290
- the method selects and replaces the first matched document. Your replace
291
- operation fails if no documents match the query filter.
292
-
293
- Example
294
- ```````
295
-
296
- The following document describes a kitchen item:
297
-
298
- .. code-block:: json
299
- :copyable: false
300
-
301
- {
302
- "_id" : 2056,
303
- "item" : "Mug",
304
- "brand" : "Simply Ceramics",
305
- "price" : 2.99,
306
- "material" : "Glass"
307
- }
308
-
309
- The following example uses the ``ReplaceOne()`` method to substitute
310
- this document with one that contains an ``item`` field with a
311
- value of "Cup" and a ``quantity`` field with a value of 107:
312
-
313
- .. io-code-block::
314
- :copyable: true
315
-
316
- .. input::
317
- :language: go
318
-
319
- filter := bson.D{{"_id", 2056}}
320
- replacement := bson.D{{"item", "Cup"}, {"quantity", 107}}
321
-
322
- result, err := collection.ReplaceOne(context.TODO(), filter, replacement)
323
- fmt.Printf("Documents matched: %v\n", result.MatchedCount)
324
- fmt.Printf("Documents replaced: %v\n", result.ModifiedCount)
325
-
326
- .. output::
327
- :language: none
328
- :visible: false
329
-
330
- Documents matched: 1
331
- Documents replaced: 1
332
-
333
- The replaced document contains the contents of the replacement document
334
- and the immutable ``_id`` field as follows:
335
-
336
- .. code-block:: json
337
- :copyable: false
338
-
339
- {
340
- "_id" : 2056,
341
- "item" : "Cup",
342
- "quantity" : 107
343
- }
344
-
345
239
Additional Information
346
240
----------------------
347
241
348
- For runnable examples of the update and replace operations, see the
349
- following usage examples:
350
-
351
- - :ref:`golang-update-one`
352
- - :ref:`golang-update-many`
353
- - :ref:`golang-replacement-document`
354
-
355
242
To learn more about the operations mentioned, see the following
356
243
guides:
357
244
358
245
- :ref:`golang-query-document`
359
- - :ref:`golang-upsert `
246
+ - :ref:`golang-retrieve `
360
247
- :ref:`golang-compound-operations`
361
248
- :manual:`Update Operators </reference/operator/update/#update-operators>`
362
249
@@ -373,4 +260,3 @@ guide, see the following API Documentation:
373
260
- `UpdateByID() <{+api+}/mongo#Collection.UpdateByID>`__
374
261
- `UpdateMany() <{+api+}/mongo#Collection.UpdateMany>`__
375
262
- `UpdateResult <{+api+}/mongo#UpdateResult>`__
376
- - `ReplaceOne() <{+api+}/mongo#Collection.ReplaceOne>`__
0 commit comments