4
4
Update Arrays in a Document
5
5
===========================
6
6
7
- .. default-domain:: mongodb
8
-
9
7
.. contents:: On this page
10
8
:local:
11
9
:backlinks: none
@@ -88,27 +86,17 @@ This example performs the following actions:
88
86
89
87
- Matches array elements in ``sizes`` where the value is less than or
90
88
equal to ``16``.
91
- - Decrements the first array value matched by ``2``.
89
+ - Decrements the first matching array value by ``2``.
92
90
93
91
.. io-code-block::
94
- :copyable: true
92
+ :copyable:
95
93
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
97
98
: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:
112
100
113
101
.. output::
114
102
:language: none
@@ -118,8 +106,9 @@ This example performs the following actions:
118
106
119
107
.. note::
120
108
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
123
112
both matched values, see :ref:`golang-multiple-elements`.
124
113
125
114
.. _golang-multiple-elements:
@@ -142,30 +131,21 @@ Example
142
131
This example performs the following actions:
143
132
144
133
- 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.
148
139
149
140
.. io-code-block::
150
141
:copyable: true
151
142
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
153
147
: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:
169
149
170
150
.. output::
171
151
:language: none
@@ -195,21 +175,12 @@ to convert from ounces to milliliters:
195
175
.. io-code-block::
196
176
:copyable: true
197
177
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
199
182
: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:
213
184
214
185
.. output::
215
186
:language: none
@@ -235,9 +206,11 @@ API Documentation
235
206
~~~~~~~~~~~~~~~~~
236
207
237
208
To 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 :
239
210
240
211
- `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>`__
243
216
- `UpdateMany() <{+api+}/mongo#Collection.UpdateMany>`__
0 commit comments