@@ -13,7 +13,7 @@ import (
1313 "go.mongodb.org/mongo-driver/v2/mongo/options"
1414)
1515
16- // start-restaurant- struct
16+ // Defines a Restaurant struct as a model for documents in the "restaurants" collection
1717type Restaurant struct {
1818 Name string
1919 RestaurantId string `bson:"restaurant_id,omitempty"`
@@ -23,8 +23,6 @@ type Restaurant struct {
2323 Grades []interface {} `bson:"grades,omitempty"`
2424}
2525
26- // end-restaurant-struct
27-
2826func main () {
2927 if err := godotenv .Load (); err != nil {
3028 log .Println ("No .env file found" )
@@ -45,29 +43,27 @@ func main() {
4543 }
4644 }()
4745
48- // begin bulk
4946 coll := client .Database ("sample_restaurants" ).Collection ("restaurants" )
5047
5148 // Creates write models that specify replace and update operations
5249 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 " }}}}),
5754 }
5855
5956 // Specifies that the bulk write is ordered
6057 opts := options .BulkWrite ().SetOrdered (true )
6158
6259 // Runs a bulk write operation for the specified write operations
6360 results , err := coll .BulkWrite (context .TODO (), models , opts )
64- // end bulk
6561
6662 if err != nil {
6763 panic (err )
6864 }
6965
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 :
7167 // Number of documents replaced or modified: 2
7268 fmt .Printf ("Number of documents replaced or modified: %d" , results .ModifiedCount )
7369}
0 commit comments