From ea067d53eaed1379d89298c5d3edfed017f9a6df Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Fri, 18 Jul 2025 11:12:17 -0400 Subject: [PATCH 1/6] DOCSP-51818 Standardize replace usage ex --- source/crud/update/replace.txt | 54 ++++++++++++++++ .../usage-examples/code-snippets/replace.go | 15 ++--- .../code-snippets/replaceBsonD.go | 61 +++++++++++++++++++ 3 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 source/includes/usage-examples/code-snippets/replaceBsonD.go diff --git a/source/crud/update/replace.txt b/source/crud/update/replace.txt index 3b65a7a3..7636af3b 100644 --- a/source/crud/update/replace.txt +++ b/source/crud/update/replace.txt @@ -120,6 +120,60 @@ and the immutable ``_id`` field as follows: "quantity" : 107 } +Replace One Document Example: Full File +--------------------------------------- + +.. include:: /includes/usage-examples/example-intro.rst + +This example performs the following actions on the ``restaurants`` +collection: + +- Matches a document in which the ``name`` is "Rizzo's Fine Pizza" +- Replaces the matched document with a new document + +Select the **Struct** or **bson.D** tab to see the corresponding code: + +.. tabs:: + + .. tab :: Struct + :tabid: structExample + + The following code uses structs to replace a document, in which the value of + ``name`` is "Rizzo's Fine Pizza", with a new document: + + .. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/replace.go + :language: go + :dedent: + + .. output:: + :language: none + :visible: false + + Number of documents replaced: 1 + + + .. tab :: bson.D + :tabid: bsonDExample + + The following code uses a bson.D type to replace a document, in which the value of + ``name`` is "Rizzo's Fine Pizza", with a new document: + + .. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/replaceBsonD.go + :language: go + :dedent: + + .. output:: + :language: none + :visible: false + + Number of documents replaced: 1 + API Documentation ----------------- diff --git a/source/includes/usage-examples/code-snippets/replace.go b/source/includes/usage-examples/code-snippets/replace.go index 84328efe..7f7a31c5 100644 --- a/source/includes/usage-examples/code-snippets/replace.go +++ b/source/includes/usage-examples/code-snippets/replace.go @@ -1,3 +1,4 @@ +// begin replace // Replaces the first document that matches a filter by using the Go driver package main @@ -8,12 +9,10 @@ import ( "os" "github.com/joho/godotenv" - "go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/v2/mongo/options" ) -// start-restaurant-struct type Restaurant struct { Name string RestaurantId string `bson:"restaurant_id,omitempty"` @@ -23,7 +22,9 @@ type Restaurant struct { Grades []interface{} `bson:"grades,omitempty"` } -// end-restaurant-struct +type RestaurantNameFilter struct { + Name string +} func main() { if err := godotenv.Load(); err != nil { @@ -45,19 +46,17 @@ func main() { } }() - // begin replace coll := client.Database("sample_restaurants").Collection("restaurants") - filter := bson.D{{"name", "Madame Vo"}} + filter := RestaurantNameFilter{Name: "Rizzo's Fine Pizza"} // Creates a new document containing "Name" and "Cuisine" fields - replacement := Restaurant{Name: "Monsieur Vo", Cuisine: "Asian Fusion"} + replacement := Restaurant{Name: "Rizzo's Pizza", Cuisine: "Pizza/American"} // Replaces the first document that matches the filter with a new document result, err := coll.ReplaceOne(context.TODO(), filter, replacement) if err != nil { panic(err) } - // end replace // Prints the number of modified documents if result.MatchedCount != 0 { @@ -67,3 +66,5 @@ func main() { // When you run this file for the first time, it should print: // Number of documents replaced: 1 } + +// end replace diff --git a/source/includes/usage-examples/code-snippets/replaceBsonD.go b/source/includes/usage-examples/code-snippets/replaceBsonD.go new file mode 100644 index 00000000..95ebde00 --- /dev/null +++ b/source/includes/usage-examples/code-snippets/replaceBsonD.go @@ -0,0 +1,61 @@ +// begin replace +// Replaces the first document that matches a filter by using the Go driver +package main + +import ( + "context" + "fmt" + "log" + "os" + + "github.com/joho/godotenv" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" +) + +func main() { + if err := godotenv.Load(); err != nil { + log.Println("No .env file found") + } + + var uri string + if uri = os.Getenv("MONGODB_URI"); uri == "" { + log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") + } + + client, err := mongo.Connect(options.Client().ApplyURI(uri)) + if err != nil { + panic(err) + } + defer func() { + if err = client.Disconnect(context.TODO()); err != nil { + panic(err) + } + }() + + coll := client.Database("sample_restaurants").Collection("restaurants") + filter := bson.D{{"name", "Rizzo's Fine Pizza"}} + + // Creates a new document containing "Name" and "Cuisine" fields + replacement := bson.D{ + bson.E{Key: "name", Value: "Rizzo's Pizza"}, + bson.E{Key: "cuisine", Value: "Pizza/American"}, + } + + // Replaces the first document that matches the filter with a new document + result, err := coll.ReplaceOne(context.TODO(), filter, replacement) + if err != nil { + panic(err) + } + + // Prints the number of modified documents + if result.MatchedCount != 0 { + fmt.Println("Number of documents replaced: %d\n", result.ModifiedCount) + } + + // When you run this file for the first time, it should print: + // Number of documents replaced: 1 +} + +// end replace From a36a4b6cb3d97aa20811e2a2695eff99765780b1 Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Fri, 18 Jul 2025 11:40:48 -0400 Subject: [PATCH 2/6] edits --- source/crud/update/replace.txt | 17 +++++++++++++++-- .../usage-examples/code-snippets/replace.go | 3 --- .../code-snippets/replaceBsonD.go | 3 --- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/source/crud/update/replace.txt b/source/crud/update/replace.txt index 7636af3b..ea07781d 100644 --- a/source/crud/update/replace.txt +++ b/source/crud/update/replace.txt @@ -128,7 +128,7 @@ Replace One Document Example: Full File This example performs the following actions on the ``restaurants`` collection: -- Matches a document in which the ``name`` is "Rizzo's Fine Pizza" +- Matches a document in which the value of ``name`` is "Rizzo's Fine Pizza" - Replaces the matched document with a new document Select the **Struct** or **bson.D** tab to see the corresponding code: @@ -154,7 +154,6 @@ Select the **Struct** or **bson.D** tab to see the corresponding code: Number of documents replaced: 1 - .. tab :: bson.D :tabid: bsonDExample @@ -174,6 +173,20 @@ Select the **Struct** or **bson.D** tab to see the corresponding code: Number of documents replaced: 1 +Expected Result +~~~~~~~~~~~~~~~ + +After you run the full example, you can find the following replaced document +in the ``restaurants`` collection: + +.. code-block:: none + + { + "_id" : ObjectId("..."), + "name" : "Rizzo's Pizza", + "cuisine" : "Pizza/American" + } + API Documentation ----------------- diff --git a/source/includes/usage-examples/code-snippets/replace.go b/source/includes/usage-examples/code-snippets/replace.go index 7f7a31c5..310c4bae 100644 --- a/source/includes/usage-examples/code-snippets/replace.go +++ b/source/includes/usage-examples/code-snippets/replace.go @@ -1,4 +1,3 @@ -// begin replace // Replaces the first document that matches a filter by using the Go driver package main @@ -66,5 +65,3 @@ func main() { // When you run this file for the first time, it should print: // Number of documents replaced: 1 } - -// end replace diff --git a/source/includes/usage-examples/code-snippets/replaceBsonD.go b/source/includes/usage-examples/code-snippets/replaceBsonD.go index 95ebde00..dfeeee15 100644 --- a/source/includes/usage-examples/code-snippets/replaceBsonD.go +++ b/source/includes/usage-examples/code-snippets/replaceBsonD.go @@ -1,4 +1,3 @@ -// begin replace // Replaces the first document that matches a filter by using the Go driver package main @@ -57,5 +56,3 @@ func main() { // When you run this file for the first time, it should print: // Number of documents replaced: 1 } - -// end replace From ac9ebdcc0537266d10e9b224f2f6a659b163f45c Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Fri, 18 Jul 2025 11:57:27 -0400 Subject: [PATCH 3/6] remove str formatting --- source/includes/usage-examples/code-snippets/replace.go | 2 +- source/includes/usage-examples/code-snippets/replaceBsonD.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/includes/usage-examples/code-snippets/replace.go b/source/includes/usage-examples/code-snippets/replace.go index 310c4bae..b38fde21 100644 --- a/source/includes/usage-examples/code-snippets/replace.go +++ b/source/includes/usage-examples/code-snippets/replace.go @@ -59,7 +59,7 @@ func main() { // Prints the number of modified documents if result.MatchedCount != 0 { - fmt.Println("Number of documents replaced: %d\n", result.ModifiedCount) + fmt.Println("Number of documents replaced:", result.ModifiedCount) } // When you run this file for the first time, it should print: diff --git a/source/includes/usage-examples/code-snippets/replaceBsonD.go b/source/includes/usage-examples/code-snippets/replaceBsonD.go index dfeeee15..d318173e 100644 --- a/source/includes/usage-examples/code-snippets/replaceBsonD.go +++ b/source/includes/usage-examples/code-snippets/replaceBsonD.go @@ -50,7 +50,7 @@ func main() { // Prints the number of modified documents if result.MatchedCount != 0 { - fmt.Println("Number of documents replaced: %d\n", result.ModifiedCount) + fmt.Println("Number of documents replaced:", result.ModifiedCount) } // When you run this file for the first time, it should print: From d0e5687dd54b81e6937289695bee02984f9ec009 Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Mon, 21 Jul 2025 19:18:05 -0400 Subject: [PATCH 4/6] SA review --- source/crud/update/replace.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/crud/update/replace.txt b/source/crud/update/replace.txt index ea07781d..87c744d6 100644 --- a/source/crud/update/replace.txt +++ b/source/crud/update/replace.txt @@ -131,7 +131,7 @@ collection: - Matches a document in which the value of ``name`` is "Rizzo's Fine Pizza" - Replaces the matched document with a new document -Select the **Struct** or **bson.D** tab to see the corresponding code: +Select the :guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code: .. tabs:: From 2a961166e4bfc0cd9d8a64e429d8052bc91bc332 Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Fri, 25 Jul 2025 17:40:26 -0400 Subject: [PATCH 5/6] tech feedback --- source/crud/update/replace.txt | 47 +++------------ .../usage-examples/code-snippets/replace.go | 7 +-- .../code-snippets/replaceBsonD.go | 58 ------------------- 3 files changed, 11 insertions(+), 101 deletions(-) delete mode 100644 source/includes/usage-examples/code-snippets/replaceBsonD.go diff --git a/source/crud/update/replace.txt b/source/crud/update/replace.txt index 87c744d6..89feca85 100644 --- a/source/crud/update/replace.txt +++ b/source/crud/update/replace.txt @@ -131,47 +131,18 @@ collection: - Matches a document in which the value of ``name`` is "Rizzo's Fine Pizza" - Replaces the matched document with a new document -Select the :guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code: - -.. tabs:: - - .. tab :: Struct - :tabid: structExample - - The following code uses structs to replace a document, in which the value of - ``name`` is "Rizzo's Fine Pizza", with a new document: - - .. io-code-block:: - :copyable: true - - .. input:: /includes/usage-examples/code-snippets/replace.go - :language: go - :dedent: - - .. output:: - :language: none - :visible: false - - Number of documents replaced: 1 - - .. tab :: bson.D - :tabid: bsonDExample - - The following code uses a bson.D type to replace a document, in which the value of - ``name`` is "Rizzo's Fine Pizza", with a new document: - - .. io-code-block:: - :copyable: true +.. io-code-block:: + :copyable: true - .. input:: /includes/usage-examples/code-snippets/replaceBsonD.go - :language: go - :dedent: + .. input:: /includes/usage-examples/code-snippets/replace.go + :language: go + :dedent: - .. output:: - :language: none - :visible: false + .. output:: + :language: none + :visible: false - Number of documents replaced: 1 + Number of documents replaced: 1 Expected Result ~~~~~~~~~~~~~~~ diff --git a/source/includes/usage-examples/code-snippets/replace.go b/source/includes/usage-examples/code-snippets/replace.go index b38fde21..5525cb94 100644 --- a/source/includes/usage-examples/code-snippets/replace.go +++ b/source/includes/usage-examples/code-snippets/replace.go @@ -8,6 +8,7 @@ import ( "os" "github.com/joho/godotenv" + "go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/v2/mongo/options" ) @@ -21,10 +22,6 @@ type Restaurant struct { Grades []interface{} `bson:"grades,omitempty"` } -type RestaurantNameFilter struct { - Name string -} - func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -46,7 +43,7 @@ func main() { }() coll := client.Database("sample_restaurants").Collection("restaurants") - filter := RestaurantNameFilter{Name: "Rizzo's Fine Pizza"} + filter := bson.D{{"name", "Rizzo's Fine Pizza"}} // Creates a new document containing "Name" and "Cuisine" fields replacement := Restaurant{Name: "Rizzo's Pizza", Cuisine: "Pizza/American"} diff --git a/source/includes/usage-examples/code-snippets/replaceBsonD.go b/source/includes/usage-examples/code-snippets/replaceBsonD.go deleted file mode 100644 index d318173e..00000000 --- a/source/includes/usage-examples/code-snippets/replaceBsonD.go +++ /dev/null @@ -1,58 +0,0 @@ -// Replaces the first document that matches a filter by using the Go driver -package main - -import ( - "context" - "fmt" - "log" - "os" - - "github.com/joho/godotenv" - "go.mongodb.org/mongo-driver/v2/bson" - "go.mongodb.org/mongo-driver/v2/mongo" - "go.mongodb.org/mongo-driver/v2/mongo/options" -) - -func main() { - if err := godotenv.Load(); err != nil { - log.Println("No .env file found") - } - - var uri string - if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") - } - - client, err := mongo.Connect(options.Client().ApplyURI(uri)) - if err != nil { - panic(err) - } - defer func() { - if err = client.Disconnect(context.TODO()); err != nil { - panic(err) - } - }() - - coll := client.Database("sample_restaurants").Collection("restaurants") - filter := bson.D{{"name", "Rizzo's Fine Pizza"}} - - // Creates a new document containing "Name" and "Cuisine" fields - replacement := bson.D{ - bson.E{Key: "name", Value: "Rizzo's Pizza"}, - bson.E{Key: "cuisine", Value: "Pizza/American"}, - } - - // Replaces the first document that matches the filter with a new document - result, err := coll.ReplaceOne(context.TODO(), filter, replacement) - if err != nil { - panic(err) - } - - // Prints the number of modified documents - if result.MatchedCount != 0 { - fmt.Println("Number of documents replaced:", result.ModifiedCount) - } - - // When you run this file for the first time, it should print: - // Number of documents replaced: 1 -} From 6e218b6f890d0f57e5009988767bed6c69d37bcf Mon Sep 17 00:00:00 2001 From: Lindsey Moore Date: Fri, 25 Jul 2025 17:42:10 -0400 Subject: [PATCH 6/6] monospace --- source/crud/update/replace.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/crud/update/replace.txt b/source/crud/update/replace.txt index 89feca85..c198d554 100644 --- a/source/crud/update/replace.txt +++ b/source/crud/update/replace.txt @@ -128,7 +128,7 @@ Replace One Document Example: Full File This example performs the following actions on the ``restaurants`` collection: -- Matches a document in which the value of ``name`` is "Rizzo's Fine Pizza" +- Matches a document in which the value of ``name`` is ``"Rizzo's Fine Pizza"`` - Replaces the matched document with a new document .. io-code-block::