Skip to content

Commit 74a5908

Browse files
authored
DOCSP-26415: use struct in bulk write usage ex (#208)
* DOCSP-26415: structs in bulkwrite ex * small fixes
1 parent fb45a07 commit 74a5908

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ import (
1212
"go.mongodb.org/mongo-driver/mongo/options"
1313
)
1414

15+
// start-restaurant-struct
16+
type Restaurant struct {
17+
Name string
18+
RestaurantId string `bson:"restaurant_id,omitempty"`
19+
Cuisine string `bson:"cuisine,omitempty"`
20+
Address interface{} `bson:"address,omitempty"`
21+
Borough string `bson:"borough,omitempty"`
22+
Grades []interface{} `bson:"grades,omitempty"`
23+
}
24+
25+
// end-restaurant-struct
26+
1527
func main() {
1628
if err := godotenv.Load(); err != nil {
1729
log.Println("No .env file found")
@@ -33,12 +45,12 @@ func main() {
3345
}()
3446

3547
// begin bulk
36-
coll := client.Database("insertDB").Collection("haikus")
48+
coll := client.Database("sample_restaurants").Collection("restaurants")
3749
models := []mongo.WriteModel{
38-
mongo.NewReplaceOneModel().SetFilter(bson.D{{"title", "Record of a Shriveled Datum"}}).
39-
SetReplacement(bson.D{{"title", "Dodging Greys"}, {"text", "When there're no matches, no longer need to panic. You can use upsert"}}),
40-
mongo.NewUpdateOneModel().SetFilter(bson.D{{"title", "Dodging Greys"}}).
41-
SetUpdate(bson.D{{"$set", bson.D{{"title", "Dodge The Greys"}}}}),
50+
mongo.NewReplaceOneModel().SetFilter(bson.D{{"name", "Cafe Tomato"}}).
51+
SetReplacement(Restaurant{Name: "Cafe Zucchini", Cuisine: "French"}),
52+
mongo.NewUpdateOneModel().SetFilter(bson.D{{"name", "Cafe Zucchini"}}).
53+
SetUpdate(bson.D{{"$set", bson.D{{"name", "Zucchini Land"}}}}),
4254
}
4355
opts := options.BulkWrite().SetOrdered(true)
4456

source/usage-examples/bulkWrite.txt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,25 @@ Example
1414

1515
.. include:: /includes/usage-examples/run-example-tip.rst
1616

17-
The following example performs the following in order on the ``haikus``
17+
This example uses the following ``Restaurant`` struct as a model for documents
18+
in the ``restaurants`` collection:
19+
20+
.. literalinclude:: /includes/usage-examples/code-snippets/bulk.go
21+
:start-after: start-restaurant-struct
22+
:end-before: end-restaurant-struct
23+
:language: go
24+
:copyable:
25+
:dedent:
26+
27+
The ``omitempty`` :ref:`struct tag<golang-struct-tags>` omits the corresponding
28+
field from the inserted document when left empty.
29+
30+
The following example performs the following in order on the ``restaurants``
1831
collection:
1932

20-
- Matches a document in which the ``title`` is "Record of a Shriveled Datum" and replace it with a new document
21-
- Matches a document in which the ``title`` is "Dodging Greys" and updates that value to "Dodge The Greys"
33+
- Matches a document in which the ``name`` is "Cafe Tomato" and replaces it with a new document
34+
- Matches a document in which the ``name`` is "Cafe Zucchini" and updates
35+
the value to "Zucchini Land"
2236

2337
.. literalinclude:: /includes/usage-examples/code-snippets/bulk.go
2438
:start-after: begin bulk
@@ -33,15 +47,15 @@ Expected Result
3347
---------------
3448

3549
After you run the full example, you can find the following document
36-
in the ``haikus`` collection:
50+
in the ``restaurants`` collection:
3751

3852
.. code-block:: json
3953
:copyable: false
4054

4155
{
42-
"_id" : ObjectId("..."),
43-
"title" : "Dodge The Greys",
44-
"text" : "When there're no matches, no longer need to panic. You can use upsert."
56+
"_id": ObjectId("..."),
57+
"name": "Zucchini Land",
58+
"cuisine": "French"
4559
}
4660

4761
For an example on how to find a document, see :ref:`golang-find-one`.

source/usage-examples/insertOne.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Example
1414

1515
.. include:: /includes/usage-examples/run-example-tip.rst
1616

17-
1817
This example uses the following ``Restaurant`` struct as a model for documents
1918
in the ``restaurants`` collection:
2019

@@ -28,7 +27,6 @@ in the ``restaurants`` collection:
2827
The ``omitempty`` :ref:`struct tag<golang-struct-tags>` omits the corresponding
2928
field from the inserted document when left empty.
3029

31-
3230
The following example inserts a new document to the ``restaurants`` collection:
3331

3432
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst

0 commit comments

Comments
 (0)