Skip to content

Commit 7b0a159

Browse files
committed
move usage example and add to guide
1 parent 8222636 commit 7b0a159

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

source/crud/bulk.txt

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,31 @@ 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 ``"Cafe Tomato"`` and
396+
replaces it with a new document
397+
- Matches a document in which the ``name`` field value is ``"Cafe Zucchini"``
398+
and updates the value to ``"Zucchini Land"``
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+
:language: none
409+
410+
Number of documents matched: 2
411+
Number of documents modified: 2
412+
388413
.. _golang-bulk-client:
389414

390415
Client Bulk Write
@@ -747,12 +772,6 @@ The following example performs the following actions in any order:
747772
Additional Information
748773
----------------------
749774

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

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

Lines changed: 2 additions & 6 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,7 +43,6 @@ 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
@@ -61,13 +58,12 @@ func main() {
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)