Skip to content

Commit f360e24

Browse files
committed
DOCSP-34174: codewhisperer pt 3 (#322)
1 parent 5ec35d3 commit f360e24

File tree

10 files changed

+45
-18
lines changed

10 files changed

+45
-18
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Deletes a document from a collection by using the Go driver
12
package main
23

34
import (
@@ -36,13 +37,18 @@ func main() {
3637
coll := client.Database("sample_mflix").Collection("movies")
3738
filter := bson.D{{"title", "Twilight"}}
3839

40+
// Deletes the first document that has a "title" value of "Twilight"
3941
result, err := coll.DeleteOne(context.TODO(), filter)
42+
43+
// Prints a message if any errors occur during the operation
4044
if err != nil {
4145
panic(err)
4246
}
4347
// end deleteOne
4448

49+
// Prints the number of deleted documents
50+
fmt.Printf("Documents deleted: %d\n", result.DeletedCount)
51+
4552
// When you run this file for the first time, it should print:
4653
// Documents deleted: 1
47-
fmt.Printf("Documents deleted: %d\n", result.DeletedCount)
4854
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Retrieves distinct values of a field by using the Go driver
12
package main
23

34
import (
@@ -36,16 +37,22 @@ func main() {
3637
coll := client.Database("sample_mflix").Collection("movies")
3738
filter := bson.D{{"directors", "Natalie Portman"}}
3839

40+
// Retrieves the distinct values of the "title" field in documents
41+
// that match the filter
3942
results, err := coll.Distinct(context.TODO(), "title", filter)
43+
44+
// Prints a message if any errors occur during the operation
4045
if err != nil {
4146
panic(err)
42-
}
47+
}
4348
// end distinct
4449

45-
// When you run this file, it should print:
46-
// A Tale of Love and Darkness
47-
// New York, I Love You
50+
// Prints the distinct "title" values
4851
for _, result := range results {
4952
fmt.Println(result)
5053
}
54+
55+
// When you run this file, it should print:
56+
// A Tale of Love and Darkness
57+
// New York, I Love You
5158
}

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

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

34
import (
@@ -49,8 +50,12 @@ func main() {
4950

5051
// begin find
5152
coll := client.Database("sample_restaurants").Collection("restaurants")
53+
54+
// Creates a query filter to match documents in which the "cuisine"
55+
// is "Italian"
5256
filter := bson.D{{"cuisine", "Italian"}}
5357

58+
// Retrieves documents that match the query filer
5459
cursor, err := coll.Find(context.TODO(), filter)
5560
if err != nil {
5661
panic(err)
@@ -62,6 +67,7 @@ func main() {
6267
panic(err)
6368
}
6469

70+
// Prints the results of the find operation as structs
6571
for _, result := range results {
6672
cursor.Decode(&result)
6773
output, err := json.MarshalIndent(result, "", " ")

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

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

34
import (
@@ -49,14 +50,19 @@ func main() {
4950

5051
// begin findOne
5152
coll := client.Database("sample_restaurants").Collection("restaurants")
53+
54+
// Creates a query filter to match documents in which the "name" is
55+
// "Bagels N Buns"
5256
filter := bson.D{{"name", "Bagels N Buns"}}
5357

58+
// Retrieves the first matching document
5459
var result Restaurant
5560
err = coll.FindOne(context.TODO(), filter).Decode(&result)
5661

62+
// Prints a message if no documents are matched or if any
63+
// other errors occur during the operation
5764
if err != nil {
5865
if err == mongo.ErrNoDocuments {
59-
// This error means your query did not match any documents.
6066
return
6167
}
6268
panic(err)

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Inserts sample documents describing restaurants by using the Go driver
12
package main
23

34
import (
@@ -45,21 +46,26 @@ func main() {
4546

4647
// begin insertMany
4748
coll := client.Database("sample_restaurants").Collection("restaurants")
49+
50+
// Creates two sample documents describing restaurants
4851
newRestaurants := []interface{}{
4952
Restaurant{Name: "Rule of Thirds", Cuisine: "Japanese"},
5053
Restaurant{Name: "Madame Vo", Cuisine: "Vietnamese"},
5154
}
5255

56+
// Inserts sample documents into the collection
5357
result, err := coll.InsertMany(context.TODO(), newRestaurants)
5458
if err != nil {
5559
panic(err)
5660
}
5761
// end insertMany
5862

59-
// When you run this file, it should print:
60-
// 2 documents inserted with IDs: ObjectID("..."), ObjectID("...")
63+
// Prints the IDs of the inserted documents
6164
fmt.Printf("%d documents inserted with IDs:\n", len(result.InsertedIDs))
6265
for _, id := range result.InsertedIDs {
6366
fmt.Printf("\t%s\n", id)
6467
}
68+
69+
// When you run this file, it should print:
70+
// 2 documents inserted with IDs: ObjectID("..."), ObjectID("...")
6571
}

source/usage-examples/deleteOne.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Delete a Document
55
=================
66

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

@@ -21,7 +19,7 @@ matched:
2119
.. literalinclude:: /includes/usage-examples/code-snippets/deleteOne.go
2220
:start-after: begin deleteOne
2321
:end-before: end deleteOne
24-
:emphasize-lines: 4
22+
:emphasize-lines: 5
2523
:language: go
2624
:dedent:
2725

@@ -37,7 +35,7 @@ in the ``movies`` collection:
3735
:copyable: false
3836

3937
// result truncated
40-
{ "_id": ObjectId("573a13bff29313caabd5e06b"), ..., "title": "Twilight", ... }
38+
{ "_id": ObjectId("..."), ..., "title": "Twilight", ... }
4139

4240
For an example on how to find a document, see :ref:`golang-find-one`.
4341

source/usage-examples/distinct.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Retrieve Distinct Values of a Field
55
===================================
66

7-
.. default-domain:: mongodb
8-
97
You can retrieve a list of distinct values for a field across a
108
collection by using the ``Distinct()`` method.
119

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

source/usage-examples/find.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ in which the ``cuisine`` is "Italian", returning all documents matched:
3232
:end-before: end find
3333
:language: go
3434
:dedent:
35-
:emphasize-lines: 4
35+
:emphasize-lines: 8
3636

3737
View a `fully runnable example <{+example+}/find.go>`__
3838

source/usage-examples/findOne.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ matched:
3333
:end-before: end findOne
3434
:language: go
3535
:dedent:
36-
:emphasize-lines: 5
36+
:emphasize-lines: 9
3737

3838
View a `fully runnable example <{+example+}/findOne.go>`__
3939

source/usage-examples/insertMany.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ collection:
3535
.. literalinclude:: /includes/usage-examples/code-snippets/insertMany.go
3636
:start-after: begin insertMany
3737
:end-before: end insertMany
38-
:emphasize-lines: 7
38+
:emphasize-lines: 10
3939
:language: go
4040
:dedent:
4141

0 commit comments

Comments
 (0)