Skip to content

Commit 90f3dcd

Browse files
committed
edit code example and RR review
1 parent b626801 commit 90f3dcd

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

source/crud/query/retrieve.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ Modify Behavior
187187
~~~~~~~~~~~~~~~
188188

189189
You can modify the behavior of ``Find()`` and ``FindOne()`` by passing
190-
in a ``FindOptions`` and ``FindOneOptions`` type. If you
190+
a ``FindOptions`` or ``FindOneOptions`` instance. If you
191191
don't specify any options, the driver uses the default values for each
192192
option.
193193

source/crud/update.txt

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Update Documents
3030
Overview
3131
--------
3232

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
3434
**update** operations.
3535

3636
Update operations change the fields that you specify while leaving other
@@ -53,14 +53,9 @@ The pattern expects you to:
5353

5454
The driver provides the following methods to update documents:
5555

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.
6459

6560
A Note About _id
6661
~~~~~~~~~~~~~~~~~
@@ -70,6 +65,11 @@ field. You cannot use update operations to change the
7065
``_id`` field. If you attempt to change this field, the update
7166
methods return a ``WriteError``.
7267

68+
Sample Data
69+
~~~~~~~~~~~
70+
71+
72+
7373
.. _golang-update-document:
7474

7575
Parameters
@@ -98,8 +98,8 @@ and descriptions </reference/operator/update-field/>`.
9898

9999
.. note:: Aggregation Pipelines in Update Operations
100100

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
103103
the aggregation stages MongoDB supports in
104104
aggregation pipelines, see our tutorial on performing
105105
:manual:`updates with aggregation pipelines
@@ -143,36 +143,41 @@ to learn how to insert a new document if no documents match the query filter.
143143
UpdateOne() Example
144144
-------------------
145145

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:
147149

148150
.. code-block:: json
149151
:copyable: false
150152

151153
{
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,
157163
...
158164
}
159165

160166
The following example uses the ``UpdateOne()`` method to:
161167

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.
166171

167172
.. io-code-block::
168173
:copyable: true
169174

170175
.. input::
171176
:language: go
172177

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}}}}
176181

177182
result, err := collection.UpdateOne(context.TODO(), filter, update)
178183
fmt.Printf("Documents matched: %v\n", result.MatchedCount)
@@ -191,23 +196,28 @@ The following shows the updated document resulting from the preceding update ope
191196
:copyable: false
192197

193198
{
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,
199208
...
200209
}
201210

202211
UpdateMany() Example
203212
--------------------
204213

205214
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>`.
208216

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``
211221

212222
.. literalinclude:: /includes/usage-examples/code-snippets/updateMany.go
213223
:start-after: begin updatemany
@@ -217,20 +227,9 @@ The example performs the following on the ``listingsAndReviews`` collection:
217227
:copyable:
218228
:dedent:
219229

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.
232231

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`.
234233

235234
Additional Information
236235
----------------------

0 commit comments

Comments
 (0)