Skip to content

Commit

Permalink
NR PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rustagir committed Nov 29, 2023
1 parent f877467 commit 1d50023
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
28 changes: 17 additions & 11 deletions source/includes/fundamentals/code-snippets/CRUD/bulkOps.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Performs a bulk writes that contain write operations
// Performs a bulk write that performs write operations
package main

import (
Expand Down Expand Up @@ -53,15 +53,16 @@ func main() {

fmt.Println("\nInsertOneModel:\n")
{
// Creates write models that specify insert operations
// Creates instructions to insert documents describing books
// begin bulk insert model
models := []mongo.WriteModel{
mongo.NewInsertOneModel().SetDocument(Book{Title: "Beloved", Author: "Toni Morrison", Length: 324}),
mongo.NewInsertOneModel().SetDocument(Book{Title: "Outline", Author: "Rachel Cusk", Length: 258}),
}
// end bulk insert model

// Performs the bulk write and prints the number of inserted documents
// Runs the bulk write operation and prints the number of
// inserted documents
results, err := coll.BulkWrite(context.TODO(), models)
if err != nil {
panic(err)
Expand Down Expand Up @@ -90,15 +91,16 @@ func main() {

fmt.Println("\nReplaceOneModel:\n")
{
// Creates a write model that specifies a replace operation
// Creates instructions to replace the first matching document
// begin bulk replace model
models := []mongo.WriteModel{
mongo.NewReplaceOneModel().SetFilter(bson.D{{"title", "Lucy"}}).
SetReplacement(Book{Title: "On Beauty", Author: "Zadie Smith", Length: 473}),
}
// end bulk replace model

// Performs the bulk write and prints the number of replaced documents
// Runs the bulk write operation and prints the number of
// replaced documents
results, err := coll.BulkWrite(context.TODO(), models)
if err != nil {
panic(err)
Expand Down Expand Up @@ -127,15 +129,16 @@ func main() {

fmt.Println("\nUpdateOneModel:\n")
{
// Creates a write model that specifies an update operation
// Creates instructions to update the first matching document
// begin bulk update model
models := []mongo.WriteModel{
mongo.NewUpdateOneModel().SetFilter(bson.D{{"author", "Elena Ferrante"}}).
SetUpdate(bson.D{{"$inc", bson.D{{"length", -15}}}}),
}
// end bulk update model

// Performs the bulk write and prints the number of updated documents
// Runs the bulk write operation and prints the number of
// updated documents
results, err := coll.BulkWrite(context.TODO(), models)
if err != nil {
panic(err)
Expand Down Expand Up @@ -164,14 +167,15 @@ func main() {

fmt.Println("\nDeleteManyModel:\n")
{
// Creates a write model that specifies a delete operation
// Creates instructions to delete all documents that match the filter
// begin bulk delete model
models := []mongo.WriteModel{
mongo.NewDeleteManyModel().SetFilter(bson.D{{"length", bson.D{{"$gt", 300}}}}),
}
// end bulk delete model

// Performs the bulk write and prints the number of deleted documents
// Runs the bulk write operation and prints the number of
// deleted documents
results, err := coll.BulkWrite(context.TODO(), models)
if err != nil {
panic(err)
Expand Down Expand Up @@ -217,7 +221,8 @@ func main() {

fmt.Println("\nBulkOperation Example:\n")
{
// Creates write models that specify multiple write operations
// Creates instructions to make changes to documents describing
// books
// begin unordered
models := []mongo.WriteModel{
mongo.NewInsertOneModel().SetDocument(Book{Title: "Middlemarch", Author: "George Eliot", Length: 904}),
Expand All @@ -232,7 +237,8 @@ func main() {
// Specifies that the bulk write is unordered
opts := options.BulkWrite().SetOrdered(false)

// Performs the bulk write and prints a summary of the data changes
// Runs the bulk write operation and prints a summary of the
// data changes
results, err := coll.BulkWrite(context.TODO(), models, opts)
if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions source/includes/usage-examples/code-snippets/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func main() {
// begin countDocuments
coll := client.Database("sample_mflix").Collection("movies")

// Specifies a filter to match documents where the "countries" value
// includes "China"
// Specifies a filter to match documents where the "countries" array
// includes a value of "China"
filter := bson.D{{"countries", "China"}}

// Retrieves and prints the estimated number of documents in the collection
Expand Down

0 comments on commit 1d50023

Please sign in to comment.