From 7b0a159f1ca3dcb840a0f0f5d0be93aa895adc9b Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Thu, 24 Jul 2025 15:24:36 -0700 Subject: [PATCH 1/6] move usage example and add to guide --- source/crud/bulk.txt | 31 +++++++++++++++---- .../usage-examples/code-snippets/bulk.go | 8 ++--- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/source/crud/bulk.txt b/source/crud/bulk.txt index 9f79301e..a1c3b1bb 100644 --- a/source/crud/bulk.txt +++ b/source/crud/bulk.txt @@ -385,6 +385,31 @@ the bulk operation: {"title":"Middlemarch","author":"George Eliot","length":904} {"title":"Pale Fire","author":"Vladimir Nabokov","length":246} +Bulk Operation Example: Full File +--------------------------------- + +.. include:: /includes/usage-examples/example-intro.rst + +The following example is a fully runnable file that performs the following actions: + +- Matches a document in which the ``name`` field value is ``"Cafe Tomato"`` and + replaces it with a new document +- Matches a document in which the ``name`` field value is ``"Cafe Zucchini"`` + and updates the value to ``"Zucchini Land"`` + +.. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/bulk.go + :language: go + :dedent: + + .. output:: + :language: none + + Number of documents matched: 2 + Number of documents modified: 2 + .. _golang-bulk-client: Client Bulk Write @@ -747,12 +772,6 @@ The following example performs the following actions in any order: Additional Information ---------------------- -For a runnable example on performing a bulk operation, see -:ref:`golang-bulk-ops-usage-example`. - -Related Operations -~~~~~~~~~~~~~~~~~~ - To learn more about performing the operations mentioned, see the following guides: diff --git a/source/includes/usage-examples/code-snippets/bulk.go b/source/includes/usage-examples/code-snippets/bulk.go index 3ae26c9f..f270c9fd 100644 --- a/source/includes/usage-examples/code-snippets/bulk.go +++ b/source/includes/usage-examples/code-snippets/bulk.go @@ -13,7 +13,7 @@ import ( "go.mongodb.org/mongo-driver/v2/mongo/options" ) -// start-restaurant-struct +// Defines a Restaurant struct as a model for documents in the "restaurants" collection type Restaurant struct { Name string RestaurantId string `bson:"restaurant_id,omitempty"` @@ -23,8 +23,6 @@ type Restaurant struct { Grades []interface{} `bson:"grades,omitempty"` } -// end-restaurant-struct - func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -45,7 +43,6 @@ func main() { } }() - // begin bulk coll := client.Database("sample_restaurants").Collection("restaurants") // Creates write models that specify replace and update operations @@ -61,13 +58,12 @@ func main() { // Runs a bulk write operation for the specified write operations results, err := coll.BulkWrite(context.TODO(), models, opts) - // end bulk if err != nil { panic(err) } - // When you run this file for the first time, it should print: + // When you run this file for the first time, it should print output similar to the following: // Number of documents replaced or modified: 2 fmt.Printf("Number of documents replaced or modified: %d", results.ModifiedCount) } From 82f6a8ae74cfbc01f96b87beb09b51aebd6d7898 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Thu, 24 Jul 2025 15:37:52 -0700 Subject: [PATCH 2/6] update directive --- source/crud/bulk.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/source/crud/bulk.txt b/source/crud/bulk.txt index a1c3b1bb..2f6572b6 100644 --- a/source/crud/bulk.txt +++ b/source/crud/bulk.txt @@ -405,6 +405,7 @@ The following example is a fully runnable file that performs the following actio :dedent: .. output:: + :visible: false :language: none Number of documents matched: 2 From 8a31a1ac0758a1f1430dfdd713fa9211ab863936 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Mon, 28 Jul 2025 11:39:30 -0700 Subject: [PATCH 3/6] refine code example feedback --- source/includes/usage-examples/code-snippets/bulk.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/includes/usage-examples/code-snippets/bulk.go b/source/includes/usage-examples/code-snippets/bulk.go index f270c9fd..70e27af4 100644 --- a/source/includes/usage-examples/code-snippets/bulk.go +++ b/source/includes/usage-examples/code-snippets/bulk.go @@ -47,10 +47,10 @@ func main() { // Creates write models that specify replace and update operations models := []mongo.WriteModel{ - mongo.NewReplaceOneModel().SetFilter(bson.D{{"name", "Cafe Tomato"}}). - SetReplacement(Restaurant{Name: "Cafe Zucchini", Cuisine: "French"}), - mongo.NewUpdateOneModel().SetFilter(bson.D{{"name", "Cafe Zucchini"}}). - SetUpdate(bson.D{{"$set", bson.D{{"name", "Zucchini Land"}}}}), + mongo.NewReplaceOneModel().SetFilter(bson.D{{"name", "Towne Cafe"}}). + SetReplacement(Restaurant{Name: "New Towne Cafe"}), + mongo.NewUpdateOneModel().SetFilter(bson.D{{"name", "New Towne Cafe"}}). + SetUpdate(bson.D{{"$set", bson.D{{"cuisine", "French"}}}}), } // Specifies that the bulk write is ordered From 375586e94f8a46f76841a0df394854d9a8eb328b Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Mon, 28 Jul 2025 11:41:54 -0700 Subject: [PATCH 4/6] update steps --- source/crud/bulk.txt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/source/crud/bulk.txt b/source/crud/bulk.txt index 2f6572b6..9a73923c 100644 --- a/source/crud/bulk.txt +++ b/source/crud/bulk.txt @@ -392,10 +392,11 @@ Bulk Operation Example: Full File The following example is a fully runnable file that performs the following actions: -- Matches a document in which the ``name`` field value is ``"Cafe Tomato"`` and - replaces it with a new document -- Matches a document in which the ``name`` field value is ``"Cafe Zucchini"`` - and updates the value to ``"Zucchini Land"`` +- Matches a document in which the ``name`` field value is ``"Towne Cafe"`` and + replaces it with a new document with the name field value set to ``"New Towne + Cafe"`` +- Matches a document in which the ``name`` field value is ``"New Towne Cafe"`` + and updates the ``cuisine`` field value to ``"French"`` .. io-code-block:: :copyable: true From 45daef8f1be9f50f460e5e014e907407aa09e266 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Mon, 28 Jul 2025 12:23:48 -0700 Subject: [PATCH 5/6] update bulk ops --- source/includes/usage-examples/code-snippets/bulk.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/includes/usage-examples/code-snippets/bulk.go b/source/includes/usage-examples/code-snippets/bulk.go index 70e27af4..7ac293ec 100644 --- a/source/includes/usage-examples/code-snippets/bulk.go +++ b/source/includes/usage-examples/code-snippets/bulk.go @@ -48,9 +48,9 @@ func main() { // Creates write models that specify replace and update operations models := []mongo.WriteModel{ mongo.NewReplaceOneModel().SetFilter(bson.D{{"name", "Towne Cafe"}}). - SetReplacement(Restaurant{Name: "New Towne Cafe"}), - mongo.NewUpdateOneModel().SetFilter(bson.D{{"name", "New Towne Cafe"}}). - SetUpdate(bson.D{{"$set", bson.D{{"cuisine", "French"}}}}), + SetReplacement(Restaurant{Name: "New Towne Cafe", Cuisine: "French"}), + mongo.NewUpdateOneModel().SetFilter(bson.D{{"name", "Riviera Caterer"}}). + SetUpdate(bson.D{{"$set", bson.D{{"name", "Riviera Cafe"}}}}), } // Specifies that the bulk write is ordered From fe1ac5bb7e1e552395d34890fde502a11db11e23 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Mon, 28 Jul 2025 12:26:40 -0700 Subject: [PATCH 6/6] update description --- source/crud/bulk.txt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/source/crud/bulk.txt b/source/crud/bulk.txt index 9a73923c..7daf6963 100644 --- a/source/crud/bulk.txt +++ b/source/crud/bulk.txt @@ -393,10 +393,11 @@ Bulk Operation Example: Full File The following example is a fully runnable file that performs the following actions: - Matches a document in which the ``name`` field value is ``"Towne Cafe"`` and - replaces it with a new document with the name field value set to ``"New Towne - Cafe"`` -- Matches a document in which the ``name`` field value is ``"New Towne Cafe"`` - and updates the ``cuisine`` field value to ``"French"`` + replaces it with a new document with the ``name`` field value set to ``"New Towne + Cafe"`` and the ``cuisine`` field value set to ``"French"`` + +- Matches a document in which the ``name`` field value is ``Riviera Caterer`` and + updates the ``name`` field value to ``"Riviera Cafe"`` .. io-code-block:: :copyable: true