2
2
.. _golang-update:
3
3
4
4
================
5
- Modify Documents
5
+ Update Documents
6
6
================
7
7
8
8
.. facet::
@@ -150,8 +150,8 @@ changes.
150
150
See our :ref:`upsert guide <golang-upsert-guide>`
151
151
to learn how to insert a new document if no documents match the query filter.
152
152
153
- Example
154
- ```````
153
+ UpdateOne() Example
154
+ -------------------
155
155
156
156
The following document describes an employee:
157
157
@@ -167,7 +167,7 @@ The following document describes an employee:
167
167
...
168
168
}
169
169
170
- The following example uses the ``UpdateByID ()`` method to:
170
+ The following example uses the ``UpdateOne ()`` method to:
171
171
172
172
- Match the document where the ``_id`` value is 2158.
173
173
- Set the ``name`` field to "Mary Wollstonecraft Shelley" and the
@@ -209,6 +209,39 @@ The following shows the updated document resulting from the preceding update ope
209
209
...
210
210
}
211
211
212
+ UpdateMany() Example
213
+ --------------------
214
+
215
+ The following example uses the ``listingsAndReviews`` collection in the
216
+ ``sample_airbnb`` dataset from the :atlas:`Atlas sample datasets </sample-data>`.
217
+ The example performs the following on the ``listingsAndReviews`` collection:
218
+
219
+ - Matches documents in which the market field of the address subdocument, ``address.market`` is "Sydney"
220
+ - Updates the ``price`` in the matched documents by 1.15 times
221
+
222
+ .. literalinclude:: /includes/usage-examples/code-snippets/updateMany.go
223
+ :start-after: begin updatemany
224
+ :end-before: end updatemany
225
+ :emphasize-lines: 9
226
+ :language: go
227
+ :copyable:
228
+ :dedent:
229
+
230
+ After you run the example, you can find the following updated
231
+ documents in the ``listingsAndReviews`` collection:
232
+
233
+ .. code-block:: json
234
+ :copyable: false
235
+
236
+ // results truncated
237
+ ...
238
+ { "_id" : "10091713", ... , "name" : "Surry Hills Studio", ... , "price" : 181.00, ... },
239
+ { "_id" : "9908871", ... , "name" : "Family friendly beach house", ... , "price" : 751.00, ... },
240
+ { "_id" : "20989061", ... , "name" : "Big and sunny Narraben room", ... , "price" : 60.00, ... },
241
+ ...
242
+
243
+ For an example on how to find multiple documents, see :ref:`golang-find-example`.
244
+
212
245
.. _golang-replacement-document:
213
246
214
247
Replace
0 commit comments