Skip to content

Commit 327a262

Browse files
committed
DOCSP-34174: codewhisperer pt 2 (#321)
* DOCSP-34174: codewhisperer pt 2 * fix highlighting * NR PR fixes
1 parent 8a54c3a commit 327a262

File tree

9 files changed

+50
-10
lines changed

9 files changed

+50
-10
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Performs a bulk write that performs write operations
12
package main
23

34
import (
@@ -52,13 +53,16 @@ func main() {
5253

5354
fmt.Println("\nInsertOneModel:\n")
5455
{
56+
// Creates instructions to insert documents describing books
5557
// begin bulk insert model
5658
models := []mongo.WriteModel{
5759
mongo.NewInsertOneModel().SetDocument(Book{Title: "Beloved", Author: "Toni Morrison", Length: 324}),
5860
mongo.NewInsertOneModel().SetDocument(Book{Title: "Outline", Author: "Rachel Cusk", Length: 258}),
5961
}
6062
// end bulk insert model
6163

64+
// Runs the bulk write operation and prints the number of
65+
// inserted documents
6266
results, err := coll.BulkWrite(context.TODO(), models)
6367
if err != nil {
6468
panic(err)
@@ -87,13 +91,16 @@ func main() {
8791

8892
fmt.Println("\nReplaceOneModel:\n")
8993
{
94+
// Creates instructions to replace the first matching document
9095
// begin bulk replace model
9196
models := []mongo.WriteModel{
9297
mongo.NewReplaceOneModel().SetFilter(bson.D{{"title", "Lucy"}}).
9398
SetReplacement(Book{Title: "On Beauty", Author: "Zadie Smith", Length: 473}),
9499
}
95100
// end bulk replace model
96101

102+
// Runs the bulk write operation and prints the number of
103+
// replaced documents
97104
results, err := coll.BulkWrite(context.TODO(), models)
98105
if err != nil {
99106
panic(err)
@@ -122,13 +129,16 @@ func main() {
122129

123130
fmt.Println("\nUpdateOneModel:\n")
124131
{
132+
// Creates instructions to update the first matching document
125133
// begin bulk update model
126134
models := []mongo.WriteModel{
127135
mongo.NewUpdateOneModel().SetFilter(bson.D{{"author", "Elena Ferrante"}}).
128136
SetUpdate(bson.D{{"$inc", bson.D{{"length", -15}}}}),
129137
}
130138
// end bulk update model
131139

140+
// Runs the bulk write operation and prints the number of
141+
// updated documents
132142
results, err := coll.BulkWrite(context.TODO(), models)
133143
if err != nil {
134144
panic(err)
@@ -157,12 +167,15 @@ func main() {
157167

158168
fmt.Println("\nDeleteManyModel:\n")
159169
{
170+
// Creates instructions to delete all documents that match the filter
160171
// begin bulk delete model
161172
models := []mongo.WriteModel{
162173
mongo.NewDeleteManyModel().SetFilter(bson.D{{"length", bson.D{{"$gt", 300}}}}),
163174
}
164175
// end bulk delete model
165176

177+
// Runs the bulk write operation and prints the number of
178+
// deleted documents
166179
results, err := coll.BulkWrite(context.TODO(), models)
167180
if err != nil {
168181
panic(err)
@@ -208,6 +221,8 @@ func main() {
208221

209222
fmt.Println("\nBulkOperation Example:\n")
210223
{
224+
// Creates instructions to make changes to documents describing
225+
// books
211226
// begin unordered
212227
models := []mongo.WriteModel{
213228
mongo.NewInsertOneModel().SetDocument(Book{Title: "Middlemarch", Author: "George Eliot", Length: 904}),
@@ -218,8 +233,12 @@ func main() {
218233
SetUpdate(bson.D{{"$inc", bson.D{{"length", 10}}}}),
219234
mongo.NewDeleteManyModel().SetFilter(bson.D{{"author", bson.D{{"$regex", "Jam"}}}}),
220235
}
236+
237+
// Specifies that the bulk write is unordered
221238
opts := options.BulkWrite().SetOrdered(false)
222239

240+
// Runs the bulk write operation and prints a summary of the
241+
// data changes
223242
results, err := coll.BulkWrite(context.TODO(), models, opts)
224243
if err != nil {
225244
panic(err)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runs bulk write operations on a collection by using the Go driver
12
package main
23

34
import (
@@ -46,14 +47,19 @@ func main() {
4647

4748
// begin bulk
4849
coll := client.Database("sample_restaurants").Collection("restaurants")
50+
51+
// Creates write models that specify replace and update operations
4952
models := []mongo.WriteModel{
5053
mongo.NewReplaceOneModel().SetFilter(bson.D{{"name", "Cafe Tomato"}}).
5154
SetReplacement(Restaurant{Name: "Cafe Zucchini", Cuisine: "French"}),
5255
mongo.NewUpdateOneModel().SetFilter(bson.D{{"name", "Cafe Zucchini"}}).
5356
SetUpdate(bson.D{{"$set", bson.D{{"name", "Zucchini Land"}}}}),
5457
}
58+
59+
// Specifies that the bulk write is ordered
5560
opts := options.BulkWrite().SetOrdered(true)
5661

62+
// Runs a bulk write operation for the specified write operations
5763
results, err := coll.BulkWrite(context.TODO(), models, opts)
5864
// end bulk
5965

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runs a database command by using the Go driver
12
package main
23

34
import (
@@ -36,10 +37,15 @@ func main() {
3637
{
3738
// begin runCommand
3839
db := client.Database("sample_restaurants")
40+
41+
// Retrieves statistics about the specified database
3942
command := bson.D{{"dbStats", 1}}
4043

4144
var result bson.M
45+
// Runs the command and prints the database statistics
4246
err := db.RunCommand(context.TODO(), command).Decode(&result)
47+
48+
// Prints a message if any errors occur during the command execution
4349
if err != nil {
4450
panic(err)
4551
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Counts documents in a collection by using the Go driver
12
package main
23

34
import (
@@ -34,16 +35,23 @@ func main() {
3435

3536
// begin countDocuments
3637
coll := client.Database("sample_mflix").Collection("movies")
38+
39+
// Specifies a filter to match documents where the "countries" array
40+
// includes a value of "China"
3741
filter := bson.D{{"countries", "China"}}
3842

43+
// Retrieves and prints the estimated number of documents in the collection
3944
estCount, estCountErr := coll.EstimatedDocumentCount(context.TODO())
4045
if estCountErr != nil {
4146
panic(estCountErr)
42-
}
47+
}
48+
49+
// Retrieves and prints the number of documents in the collection
50+
// that match the filter
4351
count, err := coll.CountDocuments(context.TODO(), filter)
4452
if err != nil {
4553
panic(err)
46-
}
54+
}
4755
// end countDocuments
4856

4957
// When you run this file, it should print:

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Deletes multiple documents from a collection by using the Go driver
12
package main
23

34
import (
@@ -36,13 +37,16 @@ func main() {
3637
coll := client.Database("sample_mflix").Collection("movies")
3738
filter := bson.D{{"runtime", bson.D{{"$gt", 800}}}}
3839

40+
// Deletes all documents that have a "runtime" value greater than 800
3941
results, err := coll.DeleteMany(context.TODO(), filter)
4042
if err != nil {
4143
panic(err)
42-
}
44+
}
4345
// end deleteMany
4446

47+
// Prints the number of deleted documents
48+
fmt.Printf("Documents deleted: %d\n", results.DeletedCount)
49+
4550
// When you run this file for the first time, it should print:
4651
// Documents deleted: 4
47-
fmt.Printf("Documents deleted: %d\n", results.DeletedCount)
4852
}

source/usage-examples/bulkWrite.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Perform Bulk Operations
55
=======================
66

7-
.. default-domain:: mongodb
8-
97
You can perform bulk write operations on a collection by using the
108
``BulkWrite()`` method.
119

@@ -37,7 +35,6 @@ collection:
3735
.. literalinclude:: /includes/usage-examples/code-snippets/bulk.go
3836
:start-after: begin bulk
3937
:end-before: end bulk
40-
:emphasize-lines: 10
4138
:language: go
4239
:dedent:
4340

source/usage-examples/command.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The following example retrieves statistics about the
2020
.. literalinclude:: /includes/usage-examples/code-snippets/command.go
2121
:start-after: begin runCommand
2222
:end-before: end runCommand
23-
:emphasize-lines: 5
23+
:emphasize-lines: 8
2424
:language: go
2525
:dedent:
2626

source/usage-examples/count.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ collection:
2525
.. literalinclude:: /includes/usage-examples/code-snippets/count.go
2626
:start-after: begin countDocuments
2727
:end-before: end countDocuments
28-
:emphasize-lines: 4, 8
28+
:emphasize-lines: 8, 15
2929
:language: go
3030
:dedent:
3131

source/usage-examples/deleteMany.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ documents matched:
2121
.. literalinclude:: /includes/usage-examples/code-snippets/deleteMany.go
2222
:start-after: begin deleteMany
2323
:end-before: end deleteMany
24-
:emphasize-lines: 4
24+
:emphasize-lines: 5
2525
:language: go
2626
:dedent:
2727

0 commit comments

Comments
 (0)