Skip to content

Commit e4d07b6

Browse files
authored
DOCSP-46724: arrayfilters removal (#418)
* DOCSP-46724: arrayfilters removal * wip
1 parent 7071ff5 commit e4d07b6

File tree

3 files changed

+49
-74
lines changed

3 files changed

+49
-74
lines changed

Diff for: source/fundamentals/crud/write-operations.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Write Operations
1313
Insert </fundamentals/crud/write-operations/insert>
1414
Delete </fundamentals/crud/write-operations/delete>
1515
Modify </fundamentals/crud/write-operations/modify>
16-
Update </fundamentals/crud/write-operations/embedded-arrays>
16+
Update Arrays </fundamentals/crud/write-operations/embedded-arrays>
1717
Upsert </fundamentals/crud/write-operations/upsert>
1818
Bulk Operations </fundamentals/crud/write-operations/bulk>
1919

Diff for: source/fundamentals/crud/write-operations/embedded-arrays.txt

+30-57
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Update 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
142131
This 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

237208
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:
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>`__

Diff for: source/includes/fundamentals/code-snippets/CRUD/updateArray.go

+18-16
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"log"
88
"os"
99

10-
"go.mongodb.org/mongo-driver/bson"
11-
"go.mongodb.org/mongo-driver/mongo"
12-
"go.mongodb.org/mongo-driver/mongo/options"
10+
"go.mongodb.org/mongo-driver/v2/bson"
11+
"go.mongodb.org/mongo-driver/v2/mongo"
12+
"go.mongodb.org/mongo-driver/v2/mongo/options"
1313
)
1414

1515
// start-drink-struct
@@ -42,11 +42,11 @@ func main() {
4242

4343
// begin insertDocs
4444
coll := client.Database("db").Collection("drinks")
45-
docs := []interface{}{
45+
docsToInsert := []interface{}{
4646
Drink{Description: "Matcha Latte", Sizes: []int32{12, 16, 20}, Styles: []string{"iced", "hot", "extra hot"}},
4747
}
4848

49-
result, err := coll.InsertMany(context.TODO(), docs)
49+
result, err := coll.InsertMany(context.TODO(), docsToInsert)
5050
//end insertDocs
5151
if err != nil {
5252
panic(err)
@@ -63,14 +63,15 @@ func main() {
6363
opts := options.FindOneAndUpdate().
6464
SetReturnDocument(options.After)
6565

66-
// Updates the first document that matches the filter and prints
67-
// the updated document as a struct
66+
// Updates the first document that matches the filter
6867
var updatedDoc Drink
69-
err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc)
68+
err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).
69+
Decode(&updatedDoc)
7070
if err != nil {
7171
panic(err)
7272
}
7373

74+
// Prints the updated document
7475
res, _ := bson.MarshalExtJSON(updatedDoc, false, false)
7576
fmt.Println(string(res))
7677
// end positional
@@ -83,7 +84,7 @@ func main() {
8384

8485
_, err := coll.UpdateOne(context.TODO(), filter, update)
8586
if err != nil {
86-
log.Fatal(err)
87+
panic(err)
8788
}
8889

8990
fmt.Println("\nData Restored\n")
@@ -97,17 +98,18 @@ func main() {
9798
identifier := []interface{}{bson.D{{"hotOptions", bson.D{{"$regex", "hot"}}}}}
9899
update := bson.D{{"$unset", bson.D{{"styles.$[hotOptions]", ""}}}}
99100
opts := options.FindOneAndUpdate().
100-
SetArrayFilters(options.ArrayFilters{Filters: identifier}).
101+
SetArrayFilters(identifier).
101102
SetReturnDocument(options.After)
102103

103-
// Updates the first document that matches the filter and prints
104-
// the updated document as a struct
104+
// Updates the first document that matches the filter
105105
var updatedDoc Drink
106-
err := coll.FindOneAndUpdate(context.TODO(), bson.D{}, update, opts).Decode(&updatedDoc)
106+
err := coll.FindOneAndUpdate(context.TODO(), bson.D{}, update, opts).
107+
Decode(&updatedDoc)
107108
if err != nil {
108109
panic(err)
109110
}
110111

112+
// Prints the updated document
111113
res, _ := bson.MarshalExtJSON(updatedDoc, false, false)
112114
fmt.Println(string(res))
113115
// end filtered positional
@@ -119,7 +121,7 @@ func main() {
119121

120122
_, err := coll.UpdateOne(context.TODO(), bson.D{}, update)
121123
if err != nil {
122-
log.Fatal(err)
124+
panic(err)
123125
}
124126

125127
fmt.Println("\nData Restored\n")
@@ -134,14 +136,14 @@ func main() {
134136
opts := options.FindOneAndUpdate().
135137
SetReturnDocument(options.After)
136138

137-
// Updates the first document that matches the filter and prints
138-
// the updated document as a struct
139+
// Updates the first document that matches the filter
139140
var updatedDoc Drink
140141
err := coll.FindOneAndUpdate(context.TODO(), bson.D{}, update, opts).Decode(&updatedDoc)
141142
if err != nil {
142143
panic(err)
143144
}
144145

146+
// Prints the updated document
145147
res, _ := bson.MarshalExtJSON(updatedDoc, false, false)
146148
fmt.Println(string(res))
147149
// end positional all

0 commit comments

Comments
 (0)