@@ -13,7 +13,7 @@ import (
13
13
"go.mongodb.org/mongo-driver/v2/mongo/options"
14
14
)
15
15
16
- // start-restaurant- struct
16
+ // Defines a Restaurant struct as a model for documents in the "restaurants" collection
17
17
type Restaurant struct {
18
18
Name string
19
19
RestaurantId string `bson:"restaurant_id,omitempty"`
@@ -23,8 +23,6 @@ type Restaurant struct {
23
23
Grades []interface {} `bson:"grades,omitempty"`
24
24
}
25
25
26
- // end-restaurant-struct
27
-
28
26
func main () {
29
27
if err := godotenv .Load (); err != nil {
30
28
log .Println ("No .env file found" )
@@ -45,29 +43,27 @@ func main() {
45
43
}
46
44
}()
47
45
48
- // begin bulk
49
46
coll := client .Database ("sample_restaurants" ).Collection ("restaurants" )
50
47
51
48
// Creates write models that specify replace and update operations
52
49
models := []mongo.WriteModel {
53
- mongo .NewReplaceOneModel ().SetFilter (bson.D {{"name" , "Cafe Tomato " }}).
54
- SetReplacement (Restaurant {Name : "Cafe Zucchini " , Cuisine : "French" }),
55
- mongo .NewUpdateOneModel ().SetFilter (bson.D {{"name" , "Cafe Zucchini " }}).
56
- SetUpdate (bson.D {{"$set" , bson.D {{"name" , "Zucchini Land " }}}}),
50
+ mongo .NewReplaceOneModel ().SetFilter (bson.D {{"name" , "Towne Cafe " }}).
51
+ SetReplacement (Restaurant {Name : "New Towne Cafe " , Cuisine : "French" }),
52
+ mongo .NewUpdateOneModel ().SetFilter (bson.D {{"name" , "Riviera Caterer " }}).
53
+ SetUpdate (bson.D {{"$set" , bson.D {{"name" , "Riviera Cafe " }}}}),
57
54
}
58
55
59
56
// Specifies that the bulk write is ordered
60
57
opts := options .BulkWrite ().SetOrdered (true )
61
58
62
59
// Runs a bulk write operation for the specified write operations
63
60
results , err := coll .BulkWrite (context .TODO (), models , opts )
64
- // end bulk
65
61
66
62
if err != nil {
67
63
panic (err )
68
64
}
69
65
70
- // When you run this file for the first time, it should print:
66
+ // When you run this file for the first time, it should print output similar to the following :
71
67
// Number of documents replaced or modified: 2
72
68
fmt .Printf ("Number of documents replaced or modified: %d" , results .ModifiedCount )
73
69
}
0 commit comments