Skip to content

Commit e27d1e3

Browse files
committed
DOCSP-34174: codewhisperer pt 7 (#326)
* DOCSP-34174: codewhisperer pt 7 * JS typo find
1 parent 28af27e commit e27d1e3

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

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

Lines changed: 6 additions & 0 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 (
@@ -58,6 +59,7 @@ func main() {
5859
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
5960

6061
{
62+
// Counts documents that match a filter and prints the count
6163
// begin count documents
6264
filter := bson.D{{"rating", bson.D{{"$lt", 6}}}}
6365

@@ -70,6 +72,8 @@ func main() {
7072
}
7173

7274
{
75+
// Estimates the total number of documents in a collection and
76+
// prints the count
7377
// begin est doc count
7478
count, err := coll.EstimatedDocumentCount(context.TODO())
7579
if err != nil {
@@ -80,6 +84,8 @@ func main() {
8084
}
8185

8286
{
87+
// Uses an aggregation pipeline to count the documents in a
88+
// collection that match the criteria
8389
// begin aggregate count
8490
matchStage := bson.D{{"$match", bson.D{{"rating", bson.D{{"$gt", 5}}}}}}
8591
countStage := bson.D{{"$count", "total_documents"}}

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

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

34
import (
@@ -79,6 +80,7 @@ func main() {
7980

8081
fmt.Println("Cursor.All():")
8182
{
83+
// Retrieves documents and references them in a cursor
8284
// begin cursor def
8385
cursor, err := coll.Find(context.TODO(), bson.D{})
8486
if err != nil {
@@ -88,6 +90,8 @@ func main() {
8890

8991
defer cursor.Close(context.TODO())
9092

93+
// Retrieves all documents from the cursor at once by unpacking
94+
// the cursor into a slice and printing the slice
9195
// begin cursor all
9296
var results []MyStruct
9397
if err = cursor.All(context.TODO(), &results); err != nil {
@@ -108,6 +112,8 @@ func main() {
108112

109113
defer cursor.Close(context.TODO())
110114

115+
// Retrieves documents from the cursor individually by iterating
116+
// through the cursor and printing each document
111117
// begin cursor next
112118
for cursor.Next(context.TODO()) {
113119
var result MyStruct
@@ -131,6 +137,8 @@ func main() {
131137

132138
defer cursor.Close(context.TODO())
133139

140+
// Retrieves documents from the tailable cursor individually by iterating
141+
// through the cursor and printing each document
134142
// begin cursor try next
135143
for {
136144
if cursor.TryNext(context.TODO()) {

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

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

34
import (
@@ -57,10 +58,17 @@ func main() {
5758

5859
fmt.Println("\nDelete Many:\n")
5960
{
61+
// Creates a filter to match documents where the "length" value
62+
// is greater than 300
6063
// begin deleteMany
6164
filter := bson.D{{"length", bson.D{{"$gt", 300}}}}
65+
66+
// Sets options for the delete operation to use the index on the
67+
// "_id" field
6268
opts := options.Delete().SetHint(bson.D{{"_id", 1}})
6369

70+
// Deletes matching documents and prints the number of deleted
71+
// documents
6472
result, err := coll.DeleteMany(context.TODO(), filter, opts)
6573
if err != nil {
6674
panic(err)

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

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

34
import (
@@ -53,6 +54,8 @@ func main() {
5354
}
5455
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
5556

57+
// Retrieves and prints distinct values of the "department" field in
58+
// documents that match the filter
5659
// begin distinct
5760
results, err := coll.Distinct(context.TODO(), "department", bson.D{{"enrollment", bson.D{{"$lt", 50}}}})
5861
if err != nil {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Sets options for insert operations by using the Go driver
12
package main
23

34
import (
@@ -15,13 +16,17 @@ type Book struct {
1516
// end-book-struct
1617

1718
func insertManyOpts() {
19+
// Sets options to bypass document validation and specify an
20+
// unordered insert when inserting multiple documents
1821
// begin insertManyOpts
1922
opts := options.InsertMany().SetBypassDocumentValidation(true).SetOrdered(false)
2023
// end insertManyOpts
2124
fmt.Println(opts)
2225
}
2326

2427
func insertOneOpts() {
28+
// Sets options to bypass document validation when inserting a
29+
// single document
2530
// begin insertOneOpts
2631
opts := options.InsertOne().SetBypassDocumentValidation(true)
2732
// end insertOneOpts

0 commit comments

Comments
 (0)