1
1
.. _golang-change-document:
2
2
.. _golang-update:
3
+ .. _golang-update-documents:
3
4
4
5
================
5
6
Update Documents
@@ -56,28 +57,23 @@ The driver provides the following methods to update documents:
56
57
* ``UpdateOne()``
57
58
* ``UpdateMany()``
58
59
59
- A Note About ``_id``
60
- ~~~~~~~~~~~~~~~~~~~~
60
+ Use the ``UpdateOne()`` or ``UpdateByID()`` method to update a single
61
+ document.
62
+
63
+ Use the ``UpdateMany()`` method to update multiple documents.
64
+
65
+ A Note About _id
66
+ ~~~~~~~~~~~~~~~~~
61
67
62
68
Each document in a MongoDB collection has a unique and immutable ``_id``
63
69
field. You cannot use update operations to change the
64
70
``_id`` field. If you attempt to change this field, the update
65
71
methods return a ``WriteError``.
66
72
67
- .. _golang-update-documents:
68
-
69
- Update
70
- ------
71
-
72
- Use the ``UpdateOne()`` or ``UpdateByID()`` method to update a single
73
- document.
74
-
75
- Use the ``UpdateMany()`` method to update multiple documents.
76
-
77
73
.. _golang-update-document:
78
74
79
75
Parameters
80
- ~~~~~~~~~~
76
+ ----------
81
77
82
78
Each method takes an **update document** that includes at least one **update operator**.
83
79
The update operator specifies the type of update to perform. The update
@@ -110,7 +106,7 @@ and descriptions </reference/operator/update-field/>`.
110
106
</tutorial/update-documents-with-aggregation-pipeline/>`.
111
107
112
108
Return Values
113
- ~~~~~~~~~~~~~
109
+ -------------
114
110
115
111
``UpdateOne()``, ``UpdateByID()``, and ``UpdateMany()`` return an
116
112
``UpdateResult`` type that contains information about the update
@@ -145,7 +141,7 @@ See our :ref:`upsert guide <golang-upsert-guide>`
145
141
to learn how to insert a new document if no documents match the query filter.
146
142
147
143
UpdateOne() Example
148
- ~~~~~~~~~~~~~~~~~~~
144
+ -------------------
149
145
150
146
The following document describes an employee:
151
147
@@ -204,7 +200,7 @@ The following shows the updated document resulting from the preceding update ope
204
200
}
205
201
206
202
UpdateMany() Example
207
- ~~~~~~~~~~~~~~~~~~~~
203
+ --------------------
208
204
209
205
The following example uses the ``listingsAndReviews`` collection in the
210
206
``sample_airbnb`` dataset from the :atlas:`Atlas sample datasets </sample-data>`.
@@ -213,13 +209,25 @@ The example performs the following on the ``listingsAndReviews`` collection:
213
209
- Matches documents in which the market field of the address subdocument, ``address.market`` is "Sydney"
214
210
- Updates the ``price`` in the matched documents by 1.15 times
215
211
216
- .. literalinclude:: /includes/usage-examples/code-snippets/updateMany.go
217
- :start-after: begin updatemany
218
- :end-before: end updatemany
219
- :emphasize-lines: 9
220
- :language: go
221
- :copyable:
222
- :dedent:
212
+ .. io-code-block::
213
+ :copyable: true
214
+
215
+ .. input::
216
+ :language: go
217
+
218
+ .. literalinclude:: /includes/usage-examples/code-snippets/updateMany.go
219
+ :start-after: begin updatemany
220
+ :end-before: end updatemany
221
+ :emphasize-lines: 9
222
+ :language: go
223
+ :copyable:
224
+ :dedent:
225
+
226
+ .. output::
227
+ :language: none
228
+ :visible: false
229
+
230
+ Documents updated: 609
223
231
224
232
After you run the example, you can find the following updated
225
233
documents in the ``listingsAndReviews`` collection:
0 commit comments