diff --git a/source/includes/fundamentals/code-snippets/CRUD/compoundOperations.go b/source/includes/fundamentals/code-snippets/CRUD/compoundOperations.go index 0618d4c4..02079698 100644 --- a/source/includes/fundamentals/code-snippets/CRUD/compoundOperations.go +++ b/source/includes/fundamentals/code-snippets/CRUD/compoundOperations.go @@ -1,3 +1,4 @@ +// Performs compound operations to find and modify data by using the Go driver package main import ( @@ -55,15 +56,19 @@ func main() { fmt.Println("\nFindOneAndDelete:\n") { + // Creates a filter to match documents where the "enrollment" + // value is less than 20 //begin FindOneAndDelete filter := bson.D{{"enrollment", bson.D{{"$lt", 20}}}} + // Finds and deletes the first matching document in one action var deletedDoc Course err := coll.FindOneAndDelete(context.TODO(), filter).Decode(&deletedDoc) if err != nil { panic(err) } + // Prints the contents of the deleted document res, _ := bson.MarshalExtJSON(deletedDoc, false, false) fmt.Println(string(res)) //end FindOneAndDelete @@ -71,17 +76,23 @@ func main() { fmt.Println("\nFindOneAndUpdate:\n") { + // Creates a filter to match documents where the "title" + // value includes the string "Modern" //begin FindOneAndUpdate filter := bson.D{{"title", bson.D{{"$regex", "Modern"}}}} + + // Creates instructions to set the "enrollment" field to 32 update := bson.D{{"$set", bson.D{{"enrollment", 32}}}} opts := options.FindOneAndUpdate().SetReturnDocument(options.After) + // Finds and updates the first matching document in one action var updatedDoc Course err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc) if err != nil { panic(err) } + // Prints the contents of the document after the update res, _ := bson.MarshalExtJSON(updatedDoc, false, false) fmt.Println(string(res)) //end FindOneAndUpdate @@ -89,16 +100,22 @@ func main() { fmt.Println("\nFindOneAndReplace:\n") { + // Creates a filter to match documents where the "title" + // value is "Representation Theory" //begin FindOneAndReplace filter := bson.D{{"title", "Representation Theory"}} + + // Creates a new document to replace the matched document replacement := Course{Title: "Combinatorial Theory", Enrollment: 35} + // Finds and replaces the first matching document in one action var previousDoc Course err := coll.FindOneAndReplace(context.TODO(), filter, replacement).Decode(&previousDoc) if err != nil { panic(err) } + // Prints the contents of the document before the replacement res, _ := bson.MarshalExtJSON(previousDoc, false, false) fmt.Println(string(res)) //end FindOneAndReplace diff --git a/source/includes/usage-examples/code-snippets/updateOne.go b/source/includes/usage-examples/code-snippets/updateOne.go index cbad2726..9d777c6f 100644 --- a/source/includes/usage-examples/code-snippets/updateOne.go +++ b/source/includes/usage-examples/code-snippets/updateOne.go @@ -1,3 +1,4 @@ +// Updates the first document that matches a query filter by using the Go driver package main import ( @@ -37,15 +38,20 @@ func main() { coll := client.Database("sample_restaurants").Collection("restaurants") id, _ := primitive.ObjectIDFromHex("5eb3d668b31de5d588f42a7a") filter := bson.D{{"_id", id}} + + // Creates instructions to add the "avg_rating" field to documents update := bson.D{{"$set", bson.D{{"avg_rating", 4.4}}}} + // Updates the first document that has the specified "_id" value result, err := coll.UpdateOne(context.TODO(), filter, update) if err != nil { panic(err) } // end updateone + // Prints the number of updated documents + fmt.Printf("Documents updated: %v\n", result.ModifiedCount) + // When you run this file for the first time, it should print: // Number of documents replaced: 1 - fmt.Printf("Documents updated: %v\n", result.ModifiedCount) } diff --git a/source/includes/usage-examples/code-snippets/watch.go b/source/includes/usage-examples/code-snippets/watch.go index ddc306a1..c0abe87f 100644 --- a/source/includes/usage-examples/code-snippets/watch.go +++ b/source/includes/usage-examples/code-snippets/watch.go @@ -1,3 +1,4 @@ +// Demonstrates how to open a change stream by using the Go driver package main import ( @@ -35,7 +36,11 @@ func main() { // begin watch coll := client.Database("sample_restaurants").Collection("restaurants") + + // Creates instructions to watch for insert operations pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{{"operationType", "insert"}}}}} + + // Creates a change stream that receives change events cs, err := coll.Watch(context.TODO(), pipeline) if err != nil { panic(err) @@ -44,6 +49,7 @@ func main() { fmt.Println("Waiting For Change Events. Insert something in MongoDB!") + // Prints a message each time the change stream receives an event for cs.Next(context.TODO()) { var event bson.M if err := cs.Decode(&event); err != nil { diff --git a/source/usage-examples/changestream.txt b/source/usage-examples/changestream.txt index a426a3ce..57d07573 100644 --- a/source/usage-examples/changestream.txt +++ b/source/usage-examples/changestream.txt @@ -22,7 +22,7 @@ and prints inserted documents: .. literalinclude:: /includes/usage-examples/code-snippets/watch.go :start-after: begin watch :end-before: end watch - :emphasize-lines: 3 + :emphasize-lines: 7 :language: go :dedent: diff --git a/source/usage-examples/updateOne.txt b/source/usage-examples/updateOne.txt index ac303ac1..dfb37c87 100644 --- a/source/usage-examples/updateOne.txt +++ b/source/usage-examples/updateOne.txt @@ -4,8 +4,6 @@ Update a Document ================= -.. default-domain:: mongodb - You can update a document in a collection by using the ``UpdateOne()`` method. @@ -23,7 +21,7 @@ collection: .. literalinclude:: /includes/usage-examples/code-snippets/updateOne.go :start-after: begin updateone :end-before: end updateone - :emphasize-lines: 6 + :emphasize-lines: 9 :language: go :dedent: