44Update Arrays in a Document
55===========================
66
7- .. default-domain:: mongodb
8-
97.. contents:: On this page
108 :local:
119 :backlinks: none
@@ -88,27 +86,17 @@ This example performs the following actions:
8886
8987- Matches array elements in ``sizes`` where the value is less than or
9088 equal to ``16``.
91- - Decrements the first array value matched by ``2``.
89+ - Decrements the first matching array value by ``2``.
9290
9391.. io-code-block::
94- :copyable: true
92+ :copyable:
9593
96- .. input::
94+ .. input:: /includes/fundamentals/code-snippets/CRUD/updateArray.go
95+ :start-after: begin positional
96+ :end-before: end positional
97+ :emphasize-lines: 2
9798 :language: go
98-
99- filter := bson.D{{"sizes", bson.D{{"$lte", 16}}}}
100- update := bson.D{{"$inc", bson.D{{"sizes.$", -2}}}}
101- opts := options.FindOneAndUpdate().
102- SetReturnDocument(options.After)
103-
104- var updatedDoc Drink
105- err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc)
106- if err != nil {
107- panic(err)
108- }
109-
110- res, _ := bson.MarshalExtJSON(updatedDoc, false, false)
111- fmt.Println(string(res))
99+ :dedent:
112100
113101 .. output::
114102 :language: none
@@ -118,8 +106,9 @@ This example performs the following actions:
118106
119107.. note::
120108
121- The query filter matches the values ``12`` and ``16``. Since the
122- operation matches ``12`` first, it is decremented. If you want to update
109+ In the preceding example, the query filter matches the values ``12``
110+ and ``16``. Because the operation matches ``12`` first, it is
111+ the target of the update. To learn how to to update
123112 both matched values, see :ref:`golang-multiple-elements`.
124113
125114.. _golang-multiple-elements:
@@ -142,30 +131,21 @@ Example
142131This example performs the following actions:
143132
144133- Creates an array filter with an identifier called ``hotOptions`` to match
145- array elements that contain "hot".
146- - Applies the array filter using the ``SetArrayFilters()`` method.
147- - Removes these array elements.
134+ array elements that contain the string ``"hot"``.
135+ - Applies the array filter by using the ``SetArrayFilters()`` method
136+ when creating a ``FindOneAndUpdateOptions`` instance.
137+ - Removes the values of these array elements by using the
138+ ``FindOneAndUpdate()`` method.
148139
149140.. io-code-block::
150141 :copyable: true
151142
152- .. input::
143+ .. input:: /includes/fundamentals/code-snippets/CRUD/updateArray.go
144+ :start-after: begin filtered positional
145+ :end-before: end filtered positional
146+ :emphasize-lines: 2, 4
153147 :language: go
154-
155- identifier := []interface{}{bson.D{{"hotOptions", bson.D{{"$regex", "hot"}}}}}
156- update := bson.D{{"$unset", bson.D{{"styles.$[hotOptions]", ""}}}}
157- opts := options.FindOneAndUpdate().
158- SetArrayFilters(options.ArrayFilters{Filters: identifier}).
159- SetReturnDocument(options.After)
160-
161- var updatedDoc Drink
162- err := coll.FindOneAndUpdate(context.TODO(), bson.D{}, update, opts).Decode(&updatedDoc)
163- if err != nil {
164- panic(err)
165- }
166-
167- res, _ := bson.MarshalExtJSON(updatedDoc, false, false)
168- fmt.Println(string(res))
148+ :dedent:
169149
170150 .. output::
171151 :language: none
@@ -195,21 +175,12 @@ to convert from ounces to milliliters:
195175.. io-code-block::
196176 :copyable: true
197177
198- .. input::
178+ .. input:: /includes/fundamentals/code-snippets/CRUD/updateArray.go
179+ :start-after: begin filtered positional
180+ :end-before: end filtered positional
181+ :emphasize-lines: 1-2
199182 :language: go
200-
201- update := bson.D{{"$mul", bson.D{{"sizes.$[]", 29.57}}}}
202- opts := options.FindOneAndUpdate().
203- SetReturnDocument(options.After)
204-
205- var updatedDoc Drink
206- err := coll.FindOneAndUpdate(context.TODO(), bson.D{}, update, opts).Decode(&updatedDoc)
207- if err != nil {
208- panic(err)
209- }
210-
211- res, _ := bson.MarshalExtJSON(updatedDoc, false, false)
212- fmt.Println(string(res))
183+ :dedent:
213184
214185 .. output::
215186 :language: none
@@ -235,9 +206,11 @@ API Documentation
235206~~~~~~~~~~~~~~~~~
236207
237208To learn more about any of the methods or types discussed in this
238- guide, see the following API Documentation :
209+ guide, see the following API documentation :
239210
240211- `FindOneAndUpdate() <{+api+}/mongo#Collection.FindOneAndUpdate>`__
241- - `FindOneAndUpdateOptions.SetReturnDocument() <{+api+}/mongo/options#FindOneAndUpdateOptions.SetReturnDocument>`__
242- - `FindOneAndUpdateOptions.SetArrayFilters() <{+api+}/mongo/options#FindOneAndUpdateOptions.SetArrayFilters>`__
212+ - `FindOneAndUpdateOptionsBuilder.SetReturnDocument()
213+ <{+api+}/mongo/options#FindOneAndUpdateOptionsBuilder.SetReturnDocument>`__
214+ - `FindOneAndUpdateOptionsBuilder.SetArrayFilters()
215+ <{+api+}/mongo/options#FindOneAndUpdateOptionsBuilder.SetArrayFilters>`__
243216- `UpdateMany() <{+api+}/mongo#Collection.UpdateMany>`__
0 commit comments