Skip to content

Commit c551dd8

Browse files
authored
DOCSP-51818 Standardize replace usage ex (#541)
* DOCSP-51818 Standardize replace usage ex * edits * remove str formatting * SA review * tech feedback * monospace
1 parent a83e137 commit c551dd8

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

source/crud/update/replace.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,44 @@ and the immutable ``_id`` field as follows:
120120
"quantity" : 107
121121
}
122122

123+
Replace One Document Example: Full File
124+
---------------------------------------
125+
126+
.. include:: /includes/usage-examples/example-intro.rst
127+
128+
This example performs the following actions on the ``restaurants``
129+
collection:
130+
131+
- Matches a document in which the value of ``name`` is ``"Rizzo's Fine Pizza"``
132+
- Replaces the matched document with a new document
133+
134+
.. io-code-block::
135+
:copyable: true
136+
137+
.. input:: /includes/usage-examples/code-snippets/replace.go
138+
:language: go
139+
:dedent:
140+
141+
.. output::
142+
:language: none
143+
:visible: false
144+
145+
Number of documents replaced: 1
146+
147+
Expected Result
148+
~~~~~~~~~~~~~~~
149+
150+
After you run the full example, you can find the following replaced document
151+
in the ``restaurants`` collection:
152+
153+
.. code-block:: none
154+
155+
{
156+
"_id" : ObjectId("..."),
157+
"name" : "Rizzo's Pizza",
158+
"cuisine" : "Pizza/American"
159+
}
160+
123161
API Documentation
124162
-----------------
125163

source/includes/usage-examples/code-snippets/replace.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"go.mongodb.org/mongo-driver/v2/mongo/options"
1414
)
1515

16-
// start-restaurant-struct
1716
type Restaurant struct {
1817
Name string
1918
RestaurantId string `bson:"restaurant_id,omitempty"`
@@ -23,8 +22,6 @@ type Restaurant struct {
2322
Grades []interface{} `bson:"grades,omitempty"`
2423
}
2524

26-
// end-restaurant-struct
27-
2825
func main() {
2926
if err := godotenv.Load(); err != nil {
3027
log.Println("No .env file found")
@@ -45,23 +42,21 @@ func main() {
4542
}
4643
}()
4744

48-
// begin replace
4945
coll := client.Database("sample_restaurants").Collection("restaurants")
50-
filter := bson.D{{"name", "Madame Vo"}}
46+
filter := bson.D{{"name", "Rizzo's Fine Pizza"}}
5147

5248
// Creates a new document containing "Name" and "Cuisine" fields
53-
replacement := Restaurant{Name: "Monsieur Vo", Cuisine: "Asian Fusion"}
49+
replacement := Restaurant{Name: "Rizzo's Pizza", Cuisine: "Pizza/American"}
5450

5551
// Replaces the first document that matches the filter with a new document
5652
result, err := coll.ReplaceOne(context.TODO(), filter, replacement)
5753
if err != nil {
5854
panic(err)
5955
}
60-
// end replace
6156

6257
// Prints the number of modified documents
6358
if result.MatchedCount != 0 {
64-
fmt.Println("Number of documents replaced: %d\n", result.ModifiedCount)
59+
fmt.Println("Number of documents replaced:", result.ModifiedCount)
6560
}
6661

6762
// When you run this file for the first time, it should print:

0 commit comments

Comments
 (0)