Skip to content

Commit a6135ff

Browse files
committed
DOCSP-34174: codewhisperer pt 8 (#327)
* DOCSP-34174: codewhisperer pt 8 * MW suggestion
1 parent 3a9eb60 commit a6135ff

File tree

7 files changed

+28
-10
lines changed

7 files changed

+28
-10
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Inserts a single document describing a restaurant by using the Go driver
12
package main
23

34
import (
@@ -43,6 +44,7 @@ func main() {
4344
}
4445
}()
4546

47+
// Inserts a sample document describing a restaurant into the collection
4648
// begin insertOne
4749
coll := client.Database("sample_restaurants").Collection("restaurants")
4850
newRestaurant := Restaurant{Name: "8282", Cuisine: "Korean"}
@@ -53,7 +55,9 @@ func main() {
5355
}
5456
// end insertOne
5557

58+
// Prints the ID of the inserted document
59+
fmt.Printf("Document inserted with ID: %s\n", result.InsertedID)
60+
5661
// When you run this file, it should print:
5762
// Document inserted with ID: ObjectID("...")
58-
fmt.Printf("Document inserted with ID: %s\n", result.InsertedID)
5963
}

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

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

34
import (
@@ -47,17 +48,22 @@ func main() {
4748
// begin replace
4849
coll := client.Database("sample_restaurants").Collection("restaurants")
4950
filter := bson.D{{"name", "Madame Vo"}}
51+
52+
// Creates a new document containing "Name" and "Cuisine" fields
5053
replacement := Restaurant{Name: "Monsieur Vo", Cuisine: "Asian Fusion"}
5154

55+
// Replaces the first document that matches the filter with a new document
5256
result, err := coll.ReplaceOne(context.TODO(), filter, replacement)
5357
if err != nil {
5458
panic(err)
5559
}
5660
// end replace
5761

58-
// When you run this file for the first time, it should print:
59-
// Number of documents replaced: 1
62+
// Prints the number of modified documents
6063
if result.MatchedCount != 0 {
6164
fmt.Println("Number of documents replaced: %d\n", result.ModifiedCount)
6265
}
66+
67+
// When you run this file for the first time, it should print:
68+
// Number of documents replaced: 1
6369
}

source/includes/usage-examples/code-snippets/struct-tag.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Specifies struct tags on a struct by using the Go driver
12
package main
23

34
import (
@@ -14,6 +15,7 @@ import (
1415
"go.mongodb.org/mongo-driver/mongo/options"
1516
)
1617

18+
// Specifies a different name for the "WordCount" field when marshalling
1719
// begin struct
1820
type BlogPost struct {
1921
Title string
@@ -57,6 +59,7 @@ func main() {
5759
Tags: []string{"seasons", "gardening", "flower"},
5860
}
5961

62+
// Inserts a document describing a blog post into the collection
6063
_, err = coll.InsertOne(context.TODO(), post)
6164
if err != nil {
6265
panic(err)
@@ -65,6 +68,8 @@ func main() {
6568

6669
filter := bson.D{{"author", "Sam Lee"}}
6770

71+
// Retrieves the inserted document and prints it as bson.M to see the
72+
// alternate field name for "WordCount"
6873
var result bson.M
6974
err = coll.FindOne(context.TODO(), filter).Decode(&result)
7075
if err != nil {

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

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

34
import (
@@ -35,15 +36,21 @@ func main() {
3536
// begin updatemany
3637
coll := client.Database("sample_airbnb").Collection("listingsAndReviews")
3738
filter := bson.D{{"address.market", "Sydney"}}
39+
40+
// Creates instructions to update the values of the "price" field
3841
update := bson.D{{"$mul", bson.D{{"price", 1.15}}}}
3942

43+
// Updates documents in which the value of the "address.market"
44+
// field is "Sydney"
4045
result, err := coll.UpdateMany(context.TODO(), filter, update)
4146
if err != nil {
4247
panic(err)
4348
}
4449
// end updatemany
4550

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

source/usage-examples/replaceOne.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ collection:
3636
.. literalinclude:: /includes/usage-examples/code-snippets/replace.go
3737
:start-after: begin replace
3838
:end-before: end replace
39-
:emphasize-lines: 5
39+
:emphasize-lines: 8
4040
:language: go
4141
:dedent:
4242

source/usage-examples/struct-tagging.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Use Struct Tags
33
===============
44

5-
.. default-domain:: mongodb
6-
75
You can specify the way that the Go Driver converts Go
86
structs to :manual:`BSON </reference/bson-types/>` by using struct tags.
97

source/usage-examples/updateMany.txt

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

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

@@ -23,7 +21,7 @@ The following example performs the following on the
2321
.. literalinclude:: /includes/usage-examples/code-snippets/updateMany.go
2422
:start-after: begin updatemany
2523
:end-before: end updatemany
26-
:emphasize-lines: 5
24+
:emphasize-lines: 9
2725
:language: go
2826
:dedent:
2927

0 commit comments

Comments
 (0)