Skip to content

Commit 984af5d

Browse files
committed
DOCSP-34174: codewhisperer pt 9 (#328)
1 parent a6135ff commit 984af5d

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Performs compound operations to find and modify data by using the Go driver
12
package main
23

34
import (
@@ -55,50 +56,66 @@ func main() {
5556

5657
fmt.Println("\nFindOneAndDelete:\n")
5758
{
59+
// Creates a filter to match documents where the "enrollment"
60+
// value is less than 20
5861
//begin FindOneAndDelete
5962
filter := bson.D{{"enrollment", bson.D{{"$lt", 20}}}}
6063

64+
// Finds and deletes the first matching document in one action
6165
var deletedDoc Course
6266
err := coll.FindOneAndDelete(context.TODO(), filter).Decode(&deletedDoc)
6367
if err != nil {
6468
panic(err)
6569
}
6670

71+
// Prints the contents of the deleted document
6772
res, _ := bson.MarshalExtJSON(deletedDoc, false, false)
6873
fmt.Println(string(res))
6974
//end FindOneAndDelete
7075
}
7176

7277
fmt.Println("\nFindOneAndUpdate:\n")
7378
{
79+
// Creates a filter to match documents where the "title"
80+
// value includes the string "Modern"
7481
//begin FindOneAndUpdate
7582
filter := bson.D{{"title", bson.D{{"$regex", "Modern"}}}}
83+
84+
// Creates instructions to set the "enrollment" field to 32
7685
update := bson.D{{"$set", bson.D{{"enrollment", 32}}}}
7786
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
7887

88+
// Finds and updates the first matching document in one action
7989
var updatedDoc Course
8090
err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc)
8191
if err != nil {
8292
panic(err)
8393
}
8494

95+
// Prints the contents of the document after the update
8596
res, _ := bson.MarshalExtJSON(updatedDoc, false, false)
8697
fmt.Println(string(res))
8798
//end FindOneAndUpdate
8899
}
89100

90101
fmt.Println("\nFindOneAndReplace:\n")
91102
{
103+
// Creates a filter to match documents where the "title"
104+
// value is "Representation Theory"
92105
//begin FindOneAndReplace
93106
filter := bson.D{{"title", "Representation Theory"}}
107+
108+
// Creates a new document to replace the matched document
94109
replacement := Course{Title: "Combinatorial Theory", Enrollment: 35}
95110

111+
// Finds and replaces the first matching document in one action
96112
var previousDoc Course
97113
err := coll.FindOneAndReplace(context.TODO(), filter, replacement).Decode(&previousDoc)
98114
if err != nil {
99115
panic(err)
100116
}
101117

118+
// Prints the contents of the document before the replacement
102119
res, _ := bson.MarshalExtJSON(previousDoc, false, false)
103120
fmt.Println(string(res))
104121
//end FindOneAndReplace

source/includes/usage-examples/code-snippets/updateOne.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Updates the first document that matches a query filter by using the Go driver
12
package main
23

34
import (
@@ -37,15 +38,20 @@ func main() {
3738
coll := client.Database("sample_restaurants").Collection("restaurants")
3839
id, _ := primitive.ObjectIDFromHex("5eb3d668b31de5d588f42a7a")
3940
filter := bson.D{{"_id", id}}
41+
42+
// Creates instructions to add the "avg_rating" field to documents
4043
update := bson.D{{"$set", bson.D{{"avg_rating", 4.4}}}}
4144

45+
// Updates the first document that has the specified "_id" value
4246
result, err := coll.UpdateOne(context.TODO(), filter, update)
4347
if err != nil {
4448
panic(err)
4549
}
4650
// end updateone
4751

52+
// Prints the number of updated documents
53+
fmt.Printf("Documents updated: %v\n", result.ModifiedCount)
54+
4855
// When you run this file for the first time, it should print:
4956
// Number of documents replaced: 1
50-
fmt.Printf("Documents updated: %v\n", result.ModifiedCount)
5157
}

source/includes/usage-examples/code-snippets/watch.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Demonstrates how to open a change stream by using the Go driver
12
package main
23

34
import (
@@ -35,7 +36,11 @@ func main() {
3536

3637
// begin watch
3738
coll := client.Database("sample_restaurants").Collection("restaurants")
39+
40+
// Creates instructions to watch for insert operations
3841
pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{{"operationType", "insert"}}}}}
42+
43+
// Creates a change stream that receives change events
3944
cs, err := coll.Watch(context.TODO(), pipeline)
4045
if err != nil {
4146
panic(err)
@@ -44,6 +49,7 @@ func main() {
4449

4550
fmt.Println("Waiting For Change Events. Insert something in MongoDB!")
4651

52+
// Prints a message each time the change stream receives an event
4753
for cs.Next(context.TODO()) {
4854
var event bson.M
4955
if err := cs.Decode(&event); err != nil {

source/usage-examples/changestream.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ and prints inserted documents:
2222
.. literalinclude:: /includes/usage-examples/code-snippets/watch.go
2323
:start-after: begin watch
2424
:end-before: end watch
25-
:emphasize-lines: 3
25+
:emphasize-lines: 7
2626
:language: go
2727
:dedent:
2828

source/usage-examples/updateOne.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Update a Document
55
=================
66

7-
.. default-domain:: mongodb
8-
97
You can update a document in a collection by using the ``UpdateOne()``
108
method.
119

@@ -23,7 +21,7 @@ collection:
2321
.. literalinclude:: /includes/usage-examples/code-snippets/updateOne.go
2422
:start-after: begin updateone
2523
:end-before: end updateone
26-
:emphasize-lines: 6
24+
:emphasize-lines: 9
2725
:language: go
2826
:dedent:
2927

0 commit comments

Comments
 (0)