diff --git a/source/includes/fundamentals/code-snippets/CRUD/runCommand.go b/source/includes/fundamentals/code-snippets/CRUD/runCommand.go index 0cce6370..b8820992 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/runCommand.go +++ b/source/includes/fundamentals/code-snippets/CRUD/runCommand.go @@ -1,3 +1,4 @@ +// Runs a database command by using the Go driver package main import ( @@ -31,9 +32,13 @@ func main() { // start-runcommand db := client.Database("plants") + + // Creates commands to count documents in a collection and explain + // 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 @@ -41,6 +46,8 @@ func main() { if err != nil { panic(err) } + + // Prints the results of the explain command output, err := json.MarshalIndent(result, "", " ") if err != nil { panic(err) diff --git a/source/includes/fundamentals/code-snippets/CRUD/skip.go b/source/includes/fundamentals/code-snippets/CRUD/skip.go index c6e93945..e946f404 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/skip.go +++ b/source/includes/fundamentals/code-snippets/CRUD/skip.go @@ -1,3 +1,5 @@ +// Skips a specified amount of documents when returning results by using +// the Go driver package main import ( @@ -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) @@ -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}} diff --git a/source/includes/fundamentals/code-snippets/CRUD/sort.go b/source/includes/fundamentals/code-snippets/CRUD/sort.go index bbec3616..92887f27 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/sort.go +++ b/source/includes/fundamentals/code-snippets/CRUD/sort.go @@ -1,3 +1,5 @@ +// Retrieves documents that match the filter and applies a +// sort to the results package main import ( @@ -56,6 +58,8 @@ func main() { fmt.Println("\nAscending Sort:\n") { + // Retrieves matching documents and sets an ascending sort on + // the "enrollment" field //begin ascending sort filter := bson.D{} opts := options.Find().SetSort(bson.D{{"enrollment", 1}}) @@ -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)) @@ -75,6 +81,8 @@ func main() { fmt.Println("\nDescending Sort:\n") { + // Retrieves matching documents and sets a descending sort on + // the "enrollment" field //begin descending sort filter := bson.D{} opts := options.Find().SetSort(bson.D{{"enrollment", -1}}) @@ -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)) @@ -94,6 +104,8 @@ func main() { fmt.Println("\nMulti Sort:\n") { + // Retrieves matching documents and sets a descending sort on + // 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}}) @@ -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)) @@ -113,6 +127,8 @@ func main() { fmt.Println("\nAggregation Sort:\n") { + // Uses an aggregation pipeline to set a descending sort on + // the "enrollment" field and an ascending sort on the "title" field // begin aggregate sort sortStage := bson.D{{"$sort", bson.D{{"enrollment", -1}, {"title", 1}}}} @@ -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)) diff --git a/source/includes/fundamentals/code-snippets/CRUD/textSearch.go b/source/includes/fundamentals/code-snippets/CRUD/textSearch.go index 8b9ca7b1..a9f5aa12 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/textSearch.go +++ b/source/includes/fundamentals/code-snippets/CRUD/textSearch.go @@ -1,3 +1,4 @@ +// Performs text searches by using the Go driver package main import ( @@ -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) @@ -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"}}}} @@ -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\""}}}} @@ -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"}}}} @@ -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"}}}} @@ -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"}}}}}} @@ -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"}}}}}} diff --git a/source/includes/fundamentals/code-snippets/CRUD/updateArray.go b/source/includes/fundamentals/code-snippets/CRUD/updateArray.go index 06ffa3bf..fca08310 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/updateArray.go +++ b/source/includes/fundamentals/code-snippets/CRUD/updateArray.go @@ -1,3 +1,4 @@ +// Updates array fields of documents by using the Go driver package main import ( @@ -54,12 +55,16 @@ func main() { fmt.Println("\nPositional $ Operator:\n") { + // Creates a filter and update document to match a "sizes" array + // value 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 { @@ -86,6 +91,8 @@ func main() { fmt.Println("\nPositional $[] Operator:\n") { + // Creates a filter and update document to match "sizes" array + // 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]", ""}}}} @@ -93,6 +100,8 @@ func main() { 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 { @@ -118,11 +127,15 @@ func main() { fmt.Println("\nPositional $[] Operator:\n") { + // Creates a filter and update document to match all "sizes" array + // values 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 {