Skip to content

Commit 0471cd4

Browse files
authored
DOCSP-26452: use struct in compound ops (#223)
* DOCSP-26452: use struct in compound ops * MW PR fixes 1
1 parent e35b3fc commit 0471cd4

File tree

3 files changed

+93
-63
lines changed

3 files changed

+93
-63
lines changed

source/fundamentals/crud/compound-operations.txt

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,35 @@ MongoDB supports the following compound operations:
3232
.. tip::
3333

3434
If you need to read and write to more than one document, use
35-
:manual:`transactions </core/transactions/>`.
35+
:ref:`transactions <golang-transactions>`.
3636

3737
Sample Data
3838
~~~~~~~~~~~
3939

40-
Run the following snippet to load the documents into the ``tea.ratings`` collection:
40+
The examples in this guide use the following ``Course`` struct as a model for documents
41+
in the ``courses`` collection:
42+
43+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/compoundOperations.go
44+
:start-after: start-course-struct
45+
:end-before: end-course-struct
46+
:language: go
47+
:dedent:
48+
49+
To run the examples in this guide, load the sample data into the
50+
``db.courses`` collection with the following
51+
snippet:
4152

4253
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/compoundOperations.go
4354
:language: go
4455
:dedent:
4556
:start-after: begin insertDocs
4657
:end-before: end insertDocs
47-
.. include:: /includes/fundamentals/tea-sample-data-ending.rst
58+
59+
Each document contains a description of a university course that
60+
includes the course title and maximum enrollment, corresponding to
61+
the ``title`` and ``enrollment`` fields in each document.
62+
63+
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst
4864

4965
.. _golang-find-and-delete:
5066

@@ -107,30 +123,32 @@ with the following methods:
107123
Example
108124
```````
109125

110-
The following example matches and deletes a document where the ``type``
111-
is "Assam" with the ``FindOneAndDelete()`` method:
126+
The following example uses the ``FindOneAndDelete()`` method
127+
to match and delete the first document where the ``enrollment``
128+
field value is less than 20:
112129

113130
.. io-code-block::
114131
:copyable: true
115132

116133
.. input::
117134
:language: go
118135

119-
filter := bson.D{{"type", "Assam"}}
136+
filter := bson.D{{"enrollment", bson.D{{"$lt", 20}}}}
120137

121-
var deletedDoc bson.D
138+
var deletedDoc Course
122139
err := coll.FindOneAndDelete(context.TODO(), filter).Decode(&deletedDoc)
123140
if err != nil {
124141
panic(err)
125142
}
126143

127-
fmt.Println(deletedDoc)
144+
res, _ := bson.MarshalExtJSON(deletedDoc, false, false)
145+
fmt.Println(string(res))
128146

129147
.. output::
130148
:language: none
131149
:visible: false
132150

133-
[{_id ObjectID("...")} {type Assam} {rating 5}]
151+
{"title":"Animal Communication","enrollment":18}
134152

135153
.. _golang-find-and-update:
136154

@@ -210,11 +228,11 @@ with the following methods:
210228
Example
211229
```````
212230

213-
The following example performs the following actions in order with the
214-
``FindOneAndUpdate()`` method:
231+
The following example uses the ``FindOneAndUpdate()`` method to
232+
perform the following actions in order:
215233

216-
- Matches a document where the ``type`` is "Oolong"
217-
- Updates the matched document's ``rating`` to ``9``
234+
- Matches the first document where the ``title`` field value includes "Modern"
235+
- Updates the matched document's ``enrollment`` field value to ``32``
218236
- Returns the updated document
219237

220238
.. io-code-block::
@@ -223,23 +241,24 @@ The following example performs the following actions in order with the
223241
.. input::
224242
:language: go
225243

226-
filter := bson.D{{"type", "Oolong"}}
227-
update := bson.D{{"$set", bson.D{{"rating", 9}}}}
244+
filter := bson.D{{"title", bson.D{{"$regex", "Modern"}}}}
245+
update := bson.D{{"$set", bson.D{{"enrollment", 32}}}}
228246
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
229-
230-
var updatedDoc bson.D
247+
248+
var updatedDoc Course
231249
err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc)
232250
if err != nil {
233-
panic(err)
251+
panic(err)
234252
}
235-
236-
fmt.Println(updatedDoc)
253+
254+
res, _ := bson.MarshalExtJSON(updatedDoc, false, false)
255+
fmt.Println(string(res))
237256

238257
.. output::
239258
:language: none
240259
:visible: false
241260

242-
[{_id ObjectID("...")} {type Oolong} {rating 9}]
261+
{"title":"Early Modern Philosophy","enrollment":32}
243262

244263
.. _golang-find-and-replace:
245264

@@ -311,34 +330,36 @@ with the following methods:
311330
Example
312331
```````
313332

314-
The following example performs the following actions in order with the
315-
``FindOneAndReplace()`` method:
333+
The following example uses the ``FindOneAndReplace()`` method to
334+
perform the following actions in order:
316335

317-
- Matches a document where the ``type`` is "English Breakfast"
318-
- Replaces the matched document with a new document where the ``type`` is "Ceylon" and ``rating`` is ``6``
336+
- Matches the first document where the ``title`` is "Representation Theory"
337+
- Replaces the matched document with a new document where the ``title``
338+
is "Combinatorial Theory" and the ``enrollment`` is ``35``
319339

320340
.. io-code-block::
321341
:copyable: true
322342

323343
.. input::
324344
:language: go
325345

326-
filter := bson.D{{"type", "English Breakfast"}}
327-
replacement := bson.D{{"type", "Ceylon"}, {"rating", 6}}
328-
329-
var previousDoc bson.D
346+
filter := bson.D{{"title", "Representation Theory"}}
347+
replacement := Course{Title: "Combinatorial Theory", Enrollment: 35}
348+
349+
var outdatedDoc Course
330350
err := coll.FindOneAndReplace(context.TODO(), filter, replacement).Decode(&previousDoc)
331351
if err != nil {
332-
panic(err)
352+
panic(err)
333353
}
334-
335-
fmt.Println(previousDoc)
354+
355+
res, _ := bson.MarshalExtJSON(outdatedDoc, false, false)
356+
fmt.Println(string(res))
336357

337358
.. output::
338359
:language: none
339360
:visible: false
340361

341-
[{_id ObjectID("...")} {type English Breakfast} {rating 5}]
362+
{"title":"Representation Theory","enrollment":40}
342363

343364
Additional Information
344365
----------------------

source/fundamentals/encrypt-fields.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ The MongoDB manual contains detailed information on the following {+qe+} topics:
5757

5858
{+csfle-long+} was introduced in MongoDB version v4.2 and supports searching encrypted
5959
fields for equality. {+csfle-short+} differs from {+qe+} in that it requires
60-
that the encrypted fields you want to search must be determinstically encrypted.
60+
that the encrypted fields you want to search must be deterministically encrypted.
6161
When you deterministically encrypt a value, the same input value produces
6262
the same output value. While deterministic encryption provides greater
6363
support for read operations, encrypted data with low :wikipedia:`cardinality <Cardinality>`

source/includes/fundamentals/code-snippets/CRUD/compoundOperations.go

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ import (
1111
"go.mongodb.org/mongo-driver/mongo/options"
1212
)
1313

14+
// start-course-struct
15+
type Course struct {
16+
Title string
17+
Enrollment int32
18+
}
19+
20+
// end-course-struct
21+
1422
func main() {
1523
var uri string
1624
if uri = os.Getenv("MONGODB_URI"); uri == "" {
@@ -28,70 +36,71 @@ func main() {
2836
}
2937
}()
3038

31-
client.Database("tea").Collection("ratings").Drop(context.TODO())
39+
client.Database("db").Collection("courses").Drop(context.TODO())
3240

3341
// begin insertDocs
34-
coll := client.Database("tea").Collection("ratings")
42+
coll := client.Database("db").Collection("courses")
3543
docs := []interface{}{
36-
bson.D{{"type", "Masala"}, {"rating", 10}},
37-
bson.D{{"type", "Assam"}, {"rating", 5}},
38-
bson.D{{"type", "Oolong"}, {"rating", 7}},
39-
bson.D{{"type", "Earl Grey"}, {"rating", 8}},
40-
bson.D{{"type", "English Breakfast"}, {"rating", 5}},
44+
Course{Title: "Representation Theory", Enrollment: 40},
45+
Course{Title: "Early Modern Philosophy", Enrollment: 25},
46+
Course{Title: "Animal Communication", Enrollment: 18},
4147
}
4248

4349
result, err := coll.InsertMany(context.TODO(), docs)
50+
//end insertDocs
4451
if err != nil {
4552
panic(err)
4653
}
4754
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
48-
//end insertDocs
4955

50-
fmt.Println("FindOneAndDelete:")
56+
fmt.Println("\nFindOneAndDelete:\n")
5157
{
5258
//begin FindOneAndDelete
53-
filter := bson.D{{"type", "Assam"}}
59+
filter := bson.D{{"enrollment", bson.D{{"$lt", 20}}}}
5460

55-
var deletedDoc bson.D
61+
var deletedDoc Course
5662
err := coll.FindOneAndDelete(context.TODO(), filter).Decode(&deletedDoc)
5763
if err != nil {
5864
panic(err)
5965
}
6066

61-
fmt.Println(deletedDoc)
67+
res, _ := bson.MarshalExtJSON(deletedDoc, false, false)
68+
fmt.Println(string(res))
6269
//end FindOneAndDelete
6370
}
6471

65-
fmt.Println("FindOneAndReplace:")
72+
fmt.Println("\nFindOneAndUpdate:\n")
6673
{
67-
//begin FindOneAndReplace
68-
filter := bson.D{{"type", "English Breakfast"}}
69-
replacement := bson.D{{"type", "Ceylon"}, {"rating", 6}}
74+
//begin FindOneAndUpdate
75+
filter := bson.D{{"title", bson.D{{"$regex", "Modern"}}}}
76+
update := bson.D{{"$set", bson.D{{"enrollment", 32}}}}
77+
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
7078

71-
var previousDoc bson.D
72-
err := coll.FindOneAndReplace(context.TODO(), filter, replacement).Decode(&previousDoc)
79+
var updatedDoc Course
80+
err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc)
7381
if err != nil {
7482
panic(err)
7583
}
7684

77-
fmt.Println(previousDoc)
78-
//end FindOneAndReplace
85+
res, _ := bson.MarshalExtJSON(updatedDoc, false, false)
86+
fmt.Println(string(res))
87+
//end FindOneAndUpdate
7988
}
8089

81-
fmt.Println("FindOneAndUpdate:")
90+
fmt.Println("\nFindOneAndReplace:\n")
8291
{
83-
//begin FindOneAndUpdate
84-
filter := bson.D{{"type", "Oolong"}}
85-
update := bson.D{{"$set", bson.D{{"rating", 9}}}}
86-
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
92+
//begin FindOneAndReplace
93+
filter := bson.D{{"title", "Representation Theory"}}
94+
replacement := Course{Title: "Combinatorial Theory", Enrollment: 35}
8795

88-
var updatedDoc bson.D
89-
err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc)
96+
var previousDoc Course
97+
err := coll.FindOneAndReplace(context.TODO(), filter, replacement).Decode(&previousDoc)
9098
if err != nil {
9199
panic(err)
92100
}
93101

94-
fmt.Println(updatedDoc)
95-
//end FindOneAndUpdate
102+
res, _ := bson.MarshalExtJSON(previousDoc, false, false)
103+
fmt.Println(string(res))
104+
//end FindOneAndReplace
96105
}
97106
}

0 commit comments

Comments
 (0)