diff --git a/source/crud/bulk.txt b/source/crud/bulk.txt index 9f79301e..7daf6963 100644 --- a/source/crud/bulk.txt +++ b/source/crud/bulk.txt @@ -385,6 +385,34 @@ the bulk operation: {"title":"Middlemarch","author":"George Eliot","length":904} {"title":"Pale Fire","author":"Vladimir Nabokov","length":246} +Bulk Operation Example: Full File +--------------------------------- + +.. include:: /includes/usage-examples/example-intro.rst + +The following example is a fully runnable file that performs the following actions: + +- Matches a document in which the ``name`` field value is ``"Towne Cafe"`` and + replaces it with a new document with the ``name`` field value set to ``"New Towne + Cafe"`` and the ``cuisine`` field value set to ``"French"`` + +- Matches a document in which the ``name`` field value is ``Riviera Caterer`` and + updates the ``name`` field value to ``"Riviera Cafe"`` + +.. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/bulk.go + :language: go + :dedent: + + .. output:: + :visible: false + :language: none + + Number of documents matched: 2 + Number of documents modified: 2 + .. _golang-bulk-client: Client Bulk Write @@ -747,12 +775,6 @@ The following example performs the following actions in any order: Additional Information ---------------------- -For a runnable example on performing a bulk operation, see -:ref:`golang-bulk-ops-usage-example`. - -Related Operations -~~~~~~~~~~~~~~~~~~ - To learn more about performing the operations mentioned, see the following guides: diff --git a/source/includes/usage-examples/code-snippets/bulk.go b/source/includes/usage-examples/code-snippets/bulk.go index 3ae26c9f..7ac293ec 100644 --- a/source/includes/usage-examples/code-snippets/bulk.go +++ b/source/includes/usage-examples/code-snippets/bulk.go @@ -13,7 +13,7 @@ import ( "go.mongodb.org/mongo-driver/v2/mongo/options" ) -// start-restaurant-struct +// Defines a Restaurant struct as a model for documents in the "restaurants" collection type Restaurant struct { Name string RestaurantId string `bson:"restaurant_id,omitempty"` @@ -23,8 +23,6 @@ type Restaurant struct { Grades []interface{} `bson:"grades,omitempty"` } -// end-restaurant-struct - func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -45,15 +43,14 @@ func main() { } }() - // begin bulk coll := client.Database("sample_restaurants").Collection("restaurants") // Creates write models that specify replace and update operations models := []mongo.WriteModel{ - mongo.NewReplaceOneModel().SetFilter(bson.D{{"name", "Cafe Tomato"}}). - SetReplacement(Restaurant{Name: "Cafe Zucchini", Cuisine: "French"}), - mongo.NewUpdateOneModel().SetFilter(bson.D{{"name", "Cafe Zucchini"}}). - SetUpdate(bson.D{{"$set", bson.D{{"name", "Zucchini Land"}}}}), + mongo.NewReplaceOneModel().SetFilter(bson.D{{"name", "Towne Cafe"}}). + SetReplacement(Restaurant{Name: "New Towne Cafe", Cuisine: "French"}), + mongo.NewUpdateOneModel().SetFilter(bson.D{{"name", "Riviera Caterer"}}). + SetUpdate(bson.D{{"$set", bson.D{{"name", "Riviera Cafe"}}}}), } // Specifies that the bulk write is ordered @@ -61,13 +58,12 @@ func main() { // Runs a bulk write operation for the specified write operations results, err := coll.BulkWrite(context.TODO(), models, opts) - // end bulk if err != nil { panic(err) } - // When you run this file for the first time, it should print: + // When you run this file for the first time, it should print output similar to the following: // Number of documents replaced or modified: 2 fmt.Printf("Number of documents replaced or modified: %d", results.ModifiedCount) }