@@ -30,7 +30,7 @@ Update Documents
30
30
Overview
31
31
--------
32
32
33
- In this guide, you can learn how to update documents in MongoDB using
33
+ In this guide, you can learn how to update documents in MongoDB by using
34
34
**update** operations.
35
35
36
36
Update operations change the fields that you specify while leaving other
@@ -53,14 +53,9 @@ The pattern expects you to:
53
53
54
54
The driver provides the following methods to update documents:
55
55
56
- * ``UpdateByID()``
57
- * ``UpdateOne()``
58
- * ``UpdateMany()``
59
-
60
- Use the ``UpdateOne()`` or ``UpdateByID()`` method to update a single
61
- document.
62
-
63
- Use the ``UpdateMany()`` method to update multiple documents.
56
+ * ``UpdateByID()``: Updates a single document based on its ``_id``.
57
+ * ``UpdateOne()``: Updates a single document.
58
+ * ``UpdateMany()``: Updates multiple documents.
64
59
65
60
A Note About _id
66
61
~~~~~~~~~~~~~~~~~
@@ -70,6 +65,11 @@ field. You cannot use update operations to change the
70
65
``_id`` field. If you attempt to change this field, the update
71
66
methods return a ``WriteError``.
72
67
68
+ Sample Data
69
+ ~~~~~~~~~~~
70
+
71
+
72
+
73
73
.. _golang-update-document:
74
74
75
75
Parameters
@@ -98,8 +98,8 @@ and descriptions </reference/operator/update-field/>`.
98
98
99
99
.. note:: Aggregation Pipelines in Update Operations
100
100
101
- If you are using MongoDB Server version 4.2 or later, you can use aggregation
102
- pipelines made up of a subset of aggregation stages in update operations. To learn more about
101
+ You can use aggregation pipelines made up of a subset of aggregation
102
+ stages in update operations. To learn more about
103
103
the aggregation stages MongoDB supports in
104
104
aggregation pipelines, see our tutorial on performing
105
105
:manual:`updates with aggregation pipelines
@@ -143,36 +143,41 @@ to learn how to insert a new document if no documents match the query filter.
143
143
UpdateOne() Example
144
144
-------------------
145
145
146
- The following document describes an employee:
146
+ The following example uses the ``listingsAndReviews`` collection in the
147
+ ``sample_airbnb`` dataset from the :atlas:`Atlas sample datasets </sample-data/sample-airbnb>`.
148
+ The following document describes an airbnb listing:
147
149
148
150
.. code-block:: json
149
151
:copyable: false
150
152
151
153
{
152
- "_id" : 2158,
153
- "name" : "Mary Shelley",
154
- "department" : "Marketing",
155
- "role" : "Marketing Analyst",
156
- "bonus" : 2500,
154
+ "_id": "10006546",
155
+ "listing_url": "https://www.airbnb.com/rooms/10006546",
156
+ "name": "Ribeira Charming Duplex",
157
+ "summary": "Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube)...",
158
+ ...
159
+ "minimum_nights": "2",
160
+ "maximum_nights": "30",
161
+ ...
162
+ "accommodates": 8,
157
163
...
158
164
}
159
165
160
166
The following example uses the ``UpdateOne()`` method to:
161
167
162
- - Match the document where the ``_id`` value is 2158.
163
- - Set the ``name`` field to "Mary Wollstonecraft Shelley" and the
164
- ``role`` field to "Marketing Director".
165
- - Increment the value of the ``bonus`` field by 2000.
168
+ - Match the document where the ``_id`` value is ``"10006546"``.
169
+ - Set the ``name`` field to "Ribiera River View Duplex".
170
+ - Increment the value of the ``accommodates`` field by 1.
166
171
167
172
.. io-code-block::
168
173
:copyable: true
169
174
170
175
.. input::
171
176
:language: go
172
177
173
- filter := bson.D{{"_id", 2158 }}
174
- update := bson.D{{"$set", bson.D{{"name", "Mary Wollstonecraft Shelley" },
175
- {"role", "Marketing Director"}}}, {" $inc", bson.D{{"bonus ", 2000 }}}}
178
+ filter := bson.D{{"_id", "10006546" }}
179
+ update := bson.D{{"$set", bson.D{{"name", "Ribiera River View Duplex"}} },
180
+ {"$inc", bson.D{{"accomodates ", 1 }}}}
176
181
177
182
result, err := collection.UpdateOne(context.TODO(), filter, update)
178
183
fmt.Printf("Documents matched: %v\n", result.MatchedCount)
@@ -191,23 +196,28 @@ The following shows the updated document resulting from the preceding update ope
191
196
:copyable: false
192
197
193
198
{
194
- "_id" : 2158,
195
- "name" : "Mary Wollstonecraft Shelley",
196
- "department" : "Marketing",
197
- "role" : "Marketing Director",
198
- "bonus" : 4500,
199
+ "_id": "10006546",
200
+ "listing_url": "https://www.airbnb.com/rooms/10006546",
201
+ "name": "Ribeira River View Duplex",
202
+ "summary": "Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube)...",
203
+ ...
204
+ "minimum_nights": "2",
205
+ "maximum_nights": "30",
206
+ ...
207
+ "accommodates": 9,
199
208
...
200
209
}
201
210
202
211
UpdateMany() Example
203
212
--------------------
204
213
205
214
The following example uses the ``listingsAndReviews`` collection in the
206
- ``sample_airbnb`` dataset from the :atlas:`Atlas sample datasets </sample-data>`.
207
- The example performs the following on the ``listingsAndReviews`` collection:
215
+ ``sample_airbnb`` dataset from the :atlas:`Atlas sample datasets </sample-data/sample-airbnb>`.
208
216
209
- - Matches documents in which the market field of the address subdocument, ``address.market`` is "Sydney"
210
- - Updates the ``price`` in the matched documents by 1.15 times
217
+ The following example uses the ``UpdateMany()`` method to:
218
+
219
+ - Match documents in which the value of the ``address.market`` field is ``"Sydney"``.
220
+ - Multiply the ``price`` value in the matched documents by ``1.15``
211
221
212
222
.. literalinclude:: /includes/usage-examples/code-snippets/updateMany.go
213
223
:start-after: begin updatemany
@@ -217,20 +227,9 @@ The example performs the following on the ``listingsAndReviews`` collection:
217
227
:copyable:
218
228
:dedent:
219
229
220
- The following query updates 609 documents. After you run the example, you can
221
- find the following updated documents in the ``listingsAndReviews`` collection:
222
-
223
- .. code-block:: json
224
- :copyable: false
225
-
226
- // results truncated
227
- ...
228
- { "_id" : "10091713", ... , "name" : "Surry Hills Studio", ... , "price" : 181.00, ... },
229
- { "_id" : "9908871", ... , "name" : "Family friendly beach house", ... , "price" : 751.00, ... },
230
- { "_id" : "20989061", ... , "name" : "Big and sunny Narraben room", ... , "price" : 60.00, ... },
231
- ...
230
+ The update operation updates ``609`` documents.
232
231
233
- For an example on how to find multiple documents, see :ref:`golang-find-example`.
232
+ To learn more about how to retrieve multiple documents, see :ref:`golang-find-example`.
234
233
235
234
Additional Information
236
235
----------------------
0 commit comments