Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOCSP-34174: codewhisperer pt 5 #324

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/runCommand.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Runs a database command by using the Go driver
package main

import (
Expand Down Expand Up @@ -31,16 +32,22 @@ func main() {

// start-runcommand
db := client.Database("plants")

// Creates commands to count documents in a collection and explain
// the how the count command runs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// the how the count command runs
// how the count command runs

countCommand := bson.D{{"count", "flowers"}}
explainCommand := bson.D{{"explain", countCommand}, {"verbosity", "queryPlanner"}}

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

if err != nil {
panic(err)
}

// Prints the results of the explain command
output, err := json.MarshalIndent(result, "", " ")
if err != nil {
panic(err)
Expand Down
6 changes: 6 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/skip.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Skips a specified amount of documents when returning results by using
// the Go driver
package main

import (
Expand Down Expand Up @@ -56,6 +58,8 @@ func main() {

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

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

fmt.Println("Aggegation Skip:")
{
// Prints all remaining documents after using an aggregation
// pipeline to apply a sort and omit the first document
// begin aggregate skip
sortStage := bson.D{{"$sort", bson.D{{"enrollment", -1}}}}
skipStage := bson.D{{"$skip", 1}}
Expand Down
18 changes: 18 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/sort.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Retrieves documents that match the filter and applies a
// sort to the results
package main

import (
Expand Down Expand Up @@ -56,6 +58,8 @@ func main() {

fmt.Println("\nAscending Sort:\n")
{
// Retrieves matching documents and sets an ascending sort on
// "enrollment"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S:

Suggested change
// "enrollment"
// the "enrollment" field

//begin ascending sort
filter := bson.D{}
opts := options.Find().SetSort(bson.D{{"enrollment", 1}})
Expand All @@ -66,6 +70,8 @@ func main() {
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}

// Prints matched documents as structs
for _, result := range results {
res, _ := json.Marshal(result)
fmt.Println(string(res))
Expand All @@ -75,6 +81,8 @@ func main() {

fmt.Println("\nDescending Sort:\n")
{
// Retrieves matching documents and sets a descending sort on
// "enrollment"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S:

Suggested change
// "enrollment"
// the "enrollment" field

//begin descending sort
filter := bson.D{}
opts := options.Find().SetSort(bson.D{{"enrollment", -1}})
Expand All @@ -85,6 +93,8 @@ func main() {
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}

// Prints matched documents as structs
for _, result := range results {
res, _ := json.Marshal(result)
fmt.Println(string(res))
Expand All @@ -94,6 +104,8 @@ func main() {

fmt.Println("\nMulti Sort:\n")
{
// Retrieves matching documents and sets a descending sort on
// "enrollment" and an ascending sort on "title"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// "enrollment" and an ascending sort on "title"
// the "enrollment" field and an ascending sort on the "title" field

//begin multi sort
filter := bson.D{}
opts := options.Find().SetSort(bson.D{{"enrollment", -1}, {"title", 1}})
Expand All @@ -104,6 +116,8 @@ func main() {
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}

// Prints matched documents as structs
for _, result := range results {
res, _ := json.Marshal(result)
fmt.Println(string(res))
Expand All @@ -113,6 +127,8 @@ func main() {

fmt.Println("\nAggregation Sort:\n")
{
// Uses an aggregation pipeline to set a descending sort on
// "enrollment" and an ascending sort on "title"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: same as above (line 108)

// begin aggregate sort
sortStage := bson.D{{"$sort", bson.D{{"enrollment", -1}, {"title", 1}}}}

Expand All @@ -125,6 +141,8 @@ func main() {
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}

// Prints matched documents as structs
for _, result := range results {
res, _ := json.Marshal(result)
fmt.Println(string(res))
Expand Down
17 changes: 17 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/textSearch.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Performs text searches by using the Go driver
package main

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

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

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

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

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

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

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

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

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

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

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

fmt.Println("\nAggregation Sort By Relevance:\n")
{
// Uses an aggregation pipeline to retrieve documents containing the "vegetarian"
// string and sorts the results by relevance based on the
// "textScore" field
// begin aggregate text score
matchStage := bson.D{{"$match", bson.D{{"$text", bson.D{{"$search", "vegetarian"}}}}}}
sortStage := bson.D{{"$sort", bson.D{{"score", bson.D{{"$meta", "textScore"}}}}}}
Expand Down
13 changes: 13 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/updateArray.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Updates array fields of documents by using the Go driver
package main

import (
Expand Down Expand Up @@ -54,12 +55,16 @@ func main() {

fmt.Println("\nPositional $ Operator:\n")
{
// Creates a filter and update document to match a "sizes" array
// entry and decrease the value by 2
// begin positional
filter := bson.D{{"sizes", bson.D{{"$lte", 16}}}}
update := bson.D{{"$inc", bson.D{{"sizes.$", -2}}}}
opts := options.FindOneAndUpdate().
SetReturnDocument(options.After)

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

fmt.Println("\nPositional $[<identifier>] Operator:\n")
{
// Creates a filter and update document to match "sizes" array
// entries and remove those values
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: just to make it a little clearer

Suggested change
// entries and remove those values
// values and remove those values

// begin filtered positional
identifier := []interface{}{bson.D{{"hotOptions", bson.D{{"$regex", "hot"}}}}}
update := bson.D{{"$unset", bson.D{{"styles.$[hotOptions]", ""}}}}
opts := options.FindOneAndUpdate().
SetArrayFilters(options.ArrayFilters{Filters: identifier}).
SetReturnDocument(options.After)

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

fmt.Println("\nPositional $[] Operator:\n")
{
// Creates a filter and update document to match all "sizes" array
// entries and multiply them by a value
// begin positional all
update := bson.D{{"$mul", bson.D{{"sizes.$[]", 29.57}}}}
opts := options.FindOneAndUpdate().
SetReturnDocument(options.After)

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