Skip to content

Commit b540bf6

Browse files
stephmarie17rachel-mack
authored andcommitted
DOCSP-51820 Move and standardize bulk operations usage example (mongodb#548)
1 parent ae0a51a commit b540bf6

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

source/crud/bulk.txt

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,34 @@ the bulk operation:
383383
{"title":"Middlemarch","author":"George Eliot","length":904}
384384
{"title":"Pale Fire","author":"Vladimir Nabokov","length":246}
385385

386+
Bulk Operation Example: Full File
387+
---------------------------------
388+
389+
.. include:: /includes/usage-examples/example-intro.rst
390+
391+
The following example is a fully runnable file that performs the following actions:
392+
393+
- Matches a document in which the ``name`` field value is ``"Towne Cafe"`` and
394+
replaces it with a new document with the ``name`` field value set to ``"New Towne
395+
Cafe"`` and the ``cuisine`` field value set to ``"French"``
396+
397+
- Matches a document in which the ``name`` field value is ``Riviera Caterer`` and
398+
updates the ``name`` field value to ``"Riviera Cafe"``
399+
400+
.. io-code-block::
401+
:copyable: true
402+
403+
.. input:: /includes/usage-examples/code-snippets/bulk.go
404+
:language: go
405+
:dedent:
406+
407+
.. output::
408+
:visible: false
409+
:language: none
410+
411+
Number of documents matched: 2
412+
Number of documents modified: 2
413+
386414
.. _golang-bulk-client:
387415

388416
Client Bulk Write
@@ -745,12 +773,6 @@ The following example performs the following actions in any order:
745773
Additional Information
746774
----------------------
747775

748-
For a runnable example on performing a bulk operation, see
749-
:ref:`golang-bulk-ops-usage-example`.
750-
751-
Related Operations
752-
~~~~~~~~~~~~~~~~~~
753-
754776
To learn more about performing the operations mentioned, see the
755777
following guides:
756778

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1717
type 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-
2826
func 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

Comments
 (0)