Skip to content

Commit 42a91d2

Browse files
committed
DOCSP-34174: codewhisperer pt 5 (#324)
* DOCSP-34174: codewhisperer pt 5 * NR PR fixes
1 parent f360e24 commit 42a91d2

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

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

Lines changed: 7 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 (
@@ -31,16 +32,22 @@ func main() {
3132

3233
// start-runcommand
3334
db := client.Database("plants")
35+
36+
// Creates commands to count documents in a collection and explain
37+
// how the count command runs
3438
countCommand := bson.D{{"count", "flowers"}}
3539
explainCommand := bson.D{{"explain", countCommand}, {"verbosity", "queryPlanner"}}
3640

41+
// Retrieves results of the explain command
3742
var result bson.M
3843
err = db.RunCommand(context.TODO(), explainCommand).Decode(&result)
3944
// end-runcommand
4045

4146
if err != nil {
4247
panic(err)
4348
}
49+
50+
// Prints the results of the explain command
4451
output, err := json.MarshalIndent(result, "", " ")
4552
if err != nil {
4653
panic(err)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Skips a specified amount of documents when returning results by using
2+
// the Go driver
13
package main
24

35
import (
@@ -56,6 +58,8 @@ func main() {
5658

5759
fmt.Println("\nSkip:\n")
5860
{
61+
// Prints all remaining documents as structs after applying an
62+
// ascending sort and omitting the first 2 documents
5963
//begin skip
6064
opts := options.Find().SetSort(bson.D{{"enrollment", 1}}).SetSkip(2)
6165

@@ -74,6 +78,8 @@ func main() {
7478

7579
fmt.Println("Aggegation Skip:")
7680
{
81+
// Prints all remaining documents after using an aggregation
82+
// pipeline to apply a sort and omit the first document
7783
// begin aggregate skip
7884
sortStage := bson.D{{"$sort", bson.D{{"enrollment", -1}}}}
7985
skipStage := bson.D{{"$skip", 1}}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Retrieves documents that match the filter and applies a
2+
// sort to the results
13
package main
24

35
import (
@@ -56,6 +58,8 @@ func main() {
5658

5759
fmt.Println("\nAscending Sort:\n")
5860
{
61+
// Retrieves matching documents and sets an ascending sort on
62+
// the "enrollment" field
5963
//begin ascending sort
6064
filter := bson.D{}
6165
opts := options.Find().SetSort(bson.D{{"enrollment", 1}})
@@ -66,6 +70,8 @@ func main() {
6670
if err = cursor.All(context.TODO(), &results); err != nil {
6771
panic(err)
6872
}
73+
74+
// Prints matched documents as structs
6975
for _, result := range results {
7076
res, _ := json.Marshal(result)
7177
fmt.Println(string(res))
@@ -75,6 +81,8 @@ func main() {
7581

7682
fmt.Println("\nDescending Sort:\n")
7783
{
84+
// Retrieves matching documents and sets a descending sort on
85+
// the "enrollment" field
7886
//begin descending sort
7987
filter := bson.D{}
8088
opts := options.Find().SetSort(bson.D{{"enrollment", -1}})
@@ -85,6 +93,8 @@ func main() {
8593
if err = cursor.All(context.TODO(), &results); err != nil {
8694
panic(err)
8795
}
96+
97+
// Prints matched documents as structs
8898
for _, result := range results {
8999
res, _ := json.Marshal(result)
90100
fmt.Println(string(res))
@@ -94,6 +104,8 @@ func main() {
94104

95105
fmt.Println("\nMulti Sort:\n")
96106
{
107+
// Retrieves matching documents and sets a descending sort on
108+
// the "enrollment" field and an ascending sort on the "title" field
97109
//begin multi sort
98110
filter := bson.D{}
99111
opts := options.Find().SetSort(bson.D{{"enrollment", -1}, {"title", 1}})
@@ -104,6 +116,8 @@ func main() {
104116
if err = cursor.All(context.TODO(), &results); err != nil {
105117
panic(err)
106118
}
119+
120+
// Prints matched documents as structs
107121
for _, result := range results {
108122
res, _ := json.Marshal(result)
109123
fmt.Println(string(res))
@@ -113,6 +127,8 @@ func main() {
113127

114128
fmt.Println("\nAggregation Sort:\n")
115129
{
130+
// Uses an aggregation pipeline to set a descending sort on
131+
// the "enrollment" field and an ascending sort on the "title" field
116132
// begin aggregate sort
117133
sortStage := bson.D{{"$sort", bson.D{{"enrollment", -1}, {"title", 1}}}}
118134

@@ -125,6 +141,8 @@ func main() {
125141
if err = cursor.All(context.TODO(), &results); err != nil {
126142
panic(err)
127143
}
144+
145+
// Prints matched documents as structs
128146
for _, result := range results {
129147
res, _ := json.Marshal(result)
130148
fmt.Println(string(res))

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Performs text searches by using the Go driver
12
package main
23

34
import (
@@ -55,6 +56,7 @@ func main() {
5556
}
5657
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
5758

59+
// Creates a text index on the "description" field
5860
//begin text index
5961
model := mongo.IndexModel{Keys: bson.D{{"description", "text"}}}
6062
name, err := coll.Indexes().CreateOne(context.TODO(), model)
@@ -67,6 +69,8 @@ func main() {
6769

6870
fmt.Println("\nTerm Search:\n")
6971
{
72+
// Retrieves and prints documents containing the "herb" string
73+
// in any fields associated with a text index
7074
//begin term search
7175
filter := bson.D{{"$text", bson.D{{"$search", "herb"}}}}
7276

@@ -88,6 +92,8 @@ func main() {
8892

8993
fmt.Println("\nPhrase Search:\n")
9094
{
95+
// Retrieves and prints documents containing the "serves 2" phrase
96+
// in any fields associated with a text index
9197
//begin phrase search
9298
filter := bson.D{{"$text", bson.D{{"$search", "\"serves 2\""}}}}
9399

@@ -109,6 +115,9 @@ func main() {
109115

110116
fmt.Println("\nExcluded Term Search:\n")
111117
{
118+
// Retrieves and prints documents containing the "vegan" but
119+
// not the "tofu" string in any fields associated with a text
120+
// index
112121
//begin exclude term search
113122
filter := bson.D{{"$text", bson.D{{"$search", "vegan -tofu"}}}}
114123

@@ -130,6 +139,9 @@ func main() {
130139

131140
fmt.Println("\nSort By Relevance:\n")
132141
{
142+
// Retrieves and prints documents containing the "vegetarian"
143+
// string and sorts the results by relevance based on the
144+
// "textScore" field
133145
//begin text score
134146
filter := bson.D{{"$text", bson.D{{"$search", "vegetarian"}}}}
135147
sort := bson.D{{"score", bson.D{{"$meta", "textScore"}}}}
@@ -153,6 +165,8 @@ func main() {
153165

154166
fmt.Println("\nAggregation Text Search:\n")
155167
{
168+
// Uses an aggregation pipeline to retrieve documents containing
169+
// the "herb" string in any fields associated with a text index
156170
// begin aggregate text search
157171
matchStage := bson.D{{"$match", bson.D{{"$text", bson.D{{"$search", "herb"}}}}}}
158172

@@ -174,6 +188,9 @@ func main() {
174188

175189
fmt.Println("\nAggregation Sort By Relevance:\n")
176190
{
191+
// Uses an aggregation pipeline to retrieve documents containing the "vegetarian"
192+
// string and sorts the results by relevance based on the
193+
// "textScore" field
177194
// begin aggregate text score
178195
matchStage := bson.D{{"$match", bson.D{{"$text", bson.D{{"$search", "vegetarian"}}}}}}
179196
sortStage := bson.D{{"$sort", bson.D{{"score", bson.D{{"$meta", "textScore"}}}}}}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Updates array fields of documents by using the Go driver
12
package main
23

34
import (
@@ -54,12 +55,16 @@ func main() {
5455

5556
fmt.Println("\nPositional $ Operator:\n")
5657
{
58+
// Creates a filter and update document to match a "sizes" array
59+
// value and decrease the value by 2
5760
// begin positional
5861
filter := bson.D{{"sizes", bson.D{{"$lte", 16}}}}
5962
update := bson.D{{"$inc", bson.D{{"sizes.$", -2}}}}
6063
opts := options.FindOneAndUpdate().
6164
SetReturnDocument(options.After)
6265

66+
// Updates the first document that matches the filter and prints
67+
// the updated document as a struct
6368
var updatedDoc Drink
6469
err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc)
6570
if err != nil {
@@ -86,13 +91,17 @@ func main() {
8691

8792
fmt.Println("\nPositional $[<identifier>] Operator:\n")
8893
{
94+
// Creates a filter and update document to match "sizes" array
95+
// values and remove those values
8996
// begin filtered positional
9097
identifier := []interface{}{bson.D{{"hotOptions", bson.D{{"$regex", "hot"}}}}}
9198
update := bson.D{{"$unset", bson.D{{"styles.$[hotOptions]", ""}}}}
9299
opts := options.FindOneAndUpdate().
93100
SetArrayFilters(options.ArrayFilters{Filters: identifier}).
94101
SetReturnDocument(options.After)
95102

103+
// Updates the first document that matches the filter and prints
104+
// the updated document as a struct
96105
var updatedDoc Drink
97106
err := coll.FindOneAndUpdate(context.TODO(), bson.D{}, update, opts).Decode(&updatedDoc)
98107
if err != nil {
@@ -118,11 +127,15 @@ func main() {
118127

119128
fmt.Println("\nPositional $[] Operator:\n")
120129
{
130+
// Creates a filter and update document to match all "sizes" array
131+
// values and multiply them by a value
121132
// begin positional all
122133
update := bson.D{{"$mul", bson.D{{"sizes.$[]", 29.57}}}}
123134
opts := options.FindOneAndUpdate().
124135
SetReturnDocument(options.After)
125136

137+
// Updates the first document that matches the filter and prints
138+
// the updated document as a struct
126139
var updatedDoc Drink
127140
err := coll.FindOneAndUpdate(context.TODO(), bson.D{}, update, opts).Decode(&updatedDoc)
128141
if err != nil {

0 commit comments

Comments
 (0)