Skip to content

DOCSP-51820 Move and standardize bulk operations usage example #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions source/crud/bulk.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand Down
16 changes: 6 additions & 10 deletions source/includes/usage-examples/code-snippets/bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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")
Expand All @@ -45,29 +43,27 @@ 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
opts := options.BulkWrite().SetOrdered(true)

// 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)
}
Loading