Skip to content

Commit ea067d5

Browse files
committed
DOCSP-51818 Standardize replace usage ex
1 parent 61b2f4c commit ea067d5

File tree

3 files changed

+123
-7
lines changed

3 files changed

+123
-7
lines changed

source/crud/update/replace.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,60 @@ 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 ``name`` is "Rizzo's Fine Pizza"
132+
- Replaces the matched document with a new document
133+
134+
Select the **Struct** or **bson.D** tab to see the corresponding code:
135+
136+
.. tabs::
137+
138+
.. tab :: Struct
139+
:tabid: structExample
140+
141+
The following code uses structs to replace a document, in which the value of
142+
``name`` is "Rizzo's Fine Pizza", with a new document:
143+
144+
.. io-code-block::
145+
:copyable: true
146+
147+
.. input:: /includes/usage-examples/code-snippets/replace.go
148+
:language: go
149+
:dedent:
150+
151+
.. output::
152+
:language: none
153+
:visible: false
154+
155+
Number of documents replaced: 1
156+
157+
158+
.. tab :: bson.D
159+
:tabid: bsonDExample
160+
161+
The following code uses a bson.D type to replace a document, in which the value of
162+
``name`` is "Rizzo's Fine Pizza", with a new document:
163+
164+
.. io-code-block::
165+
:copyable: true
166+
167+
.. input:: /includes/usage-examples/code-snippets/replaceBsonD.go
168+
:language: go
169+
:dedent:
170+
171+
.. output::
172+
:language: none
173+
:visible: false
174+
175+
Number of documents replaced: 1
176+
123177
API Documentation
124178
-----------------
125179

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// begin replace
12
// Replaces the first document that matches a filter by using the Go driver
23
package main
34

@@ -8,12 +9,10 @@ import (
89
"os"
910

1011
"github.com/joho/godotenv"
11-
"go.mongodb.org/mongo-driver/v2/bson"
1212
"go.mongodb.org/mongo-driver/v2/mongo"
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,7 +22,9 @@ type Restaurant struct {
2322
Grades []interface{} `bson:"grades,omitempty"`
2423
}
2524

26-
// end-restaurant-struct
25+
type RestaurantNameFilter struct {
26+
Name string
27+
}
2728

2829
func main() {
2930
if err := godotenv.Load(); err != nil {
@@ -45,19 +46,17 @@ func main() {
4546
}
4647
}()
4748

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

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

5555
// Replaces the first document that matches the filter with a new document
5656
result, err := coll.ReplaceOne(context.TODO(), filter, replacement)
5757
if err != nil {
5858
panic(err)
5959
}
60-
// end replace
6160

6261
// Prints the number of modified documents
6362
if result.MatchedCount != 0 {
@@ -67,3 +66,5 @@ func main() {
6766
// When you run this file for the first time, it should print:
6867
// Number of documents replaced: 1
6968
}
69+
70+
// end replace
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// begin replace
2+
// Replaces the first document that matches a filter by using the Go driver
3+
package main
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"log"
9+
"os"
10+
11+
"github.com/joho/godotenv"
12+
"go.mongodb.org/mongo-driver/v2/bson"
13+
"go.mongodb.org/mongo-driver/v2/mongo"
14+
"go.mongodb.org/mongo-driver/v2/mongo/options"
15+
)
16+
17+
func main() {
18+
if err := godotenv.Load(); err != nil {
19+
log.Println("No .env file found")
20+
}
21+
22+
var uri string
23+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
24+
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")
25+
}
26+
27+
client, err := mongo.Connect(options.Client().ApplyURI(uri))
28+
if err != nil {
29+
panic(err)
30+
}
31+
defer func() {
32+
if err = client.Disconnect(context.TODO()); err != nil {
33+
panic(err)
34+
}
35+
}()
36+
37+
coll := client.Database("sample_restaurants").Collection("restaurants")
38+
filter := bson.D{{"name", "Rizzo's Fine Pizza"}}
39+
40+
// Creates a new document containing "Name" and "Cuisine" fields
41+
replacement := bson.D{
42+
bson.E{Key: "name", Value: "Rizzo's Pizza"},
43+
bson.E{Key: "cuisine", Value: "Pizza/American"},
44+
}
45+
46+
// Replaces the first document that matches the filter with a new document
47+
result, err := coll.ReplaceOne(context.TODO(), filter, replacement)
48+
if err != nil {
49+
panic(err)
50+
}
51+
52+
// Prints the number of modified documents
53+
if result.MatchedCount != 0 {
54+
fmt.Println("Number of documents replaced: %d\n", result.ModifiedCount)
55+
}
56+
57+
// When you run this file for the first time, it should print:
58+
// Number of documents replaced: 1
59+
}
60+
61+
// end replace

0 commit comments

Comments
 (0)