Skip to content

Commit 1ca2819

Browse files
authored
DOCSP-51820 Move and standardize bulk operations usage example (#548)
1 parent 49611a8 commit 1ca2819

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
@@ -385,6 +385,34 @@ the bulk operation:
385385
{"title":"Middlemarch","author":"George Eliot","length":904}
386386
{"title":"Pale Fire","author":"Vladimir Nabokov","length":246}
387387

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

390418
Client Bulk Write
@@ -747,12 +775,6 @@ The following example performs the following actions in any order:
747775
Additional Information
748776
----------------------
749777

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

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)