Skip to content

Commit 69da7b9

Browse files
authored
DOCSP-26413 Update Insert Many Usage Example Structs (#194)
* DOCSP-26413 Update Insert Many Usage Example Structs * typo fix * review edits
1 parent f48f75a commit 69da7b9

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,22 @@ import (
77
"os"
88

99
"github.com/joho/godotenv"
10-
"go.mongodb.org/mongo-driver/bson"
1110
"go.mongodb.org/mongo-driver/mongo"
1211
"go.mongodb.org/mongo-driver/mongo/options"
1312
)
1413

14+
// start-restaurant-struct
15+
type Restaurant struct {
16+
Name string
17+
RestaurantId string `bson:"restaurant_id,omitempty"`
18+
Cuisine string
19+
Address interface{} `bson:"address,omitempty"`
20+
Borough string
21+
Grades []interface{} `bson:"grades,omitempty"`
22+
}
23+
24+
// end-restaurant-struct
25+
1526
func main() {
1627
if err := godotenv.Load(); err != nil {
1728
log.Println("No .env file found")
@@ -33,20 +44,21 @@ func main() {
3344
}()
3445

3546
// begin insertMany
36-
coll := client.Database("insertDB").Collection("haikus")
37-
docs := []interface{}{
38-
bson.D{{"title", "Record of a Shriveled Datum"}, {"text", "No bytes, no problem. Just insert a document, in MongoDB"}},
39-
bson.D{{"title", "Showcasing a Blossoming Binary"}, {"text", "Binary data, safely stored with GridFS. Bucket the data"}},
47+
coll := client.Database("sample_restaurants").Collection("restaurants")
48+
newRestaurants := []interface{}{
49+
Restaurant{Name: "Rule of Thirds", Cuisine: "Japanese", Borough: "Brooklyn"},
50+
Restaurant{Name: "Soothr", Cuisine: "Thai", Borough: "Manhattan"},
51+
Restaurant{Name: "Madame Vo", Cuisine: "Vietnamese", Borough: "Manhattan"},
4052
}
4153

42-
result, err := coll.InsertMany(context.TODO(), docs)
54+
result, err := coll.InsertMany(context.TODO(), newRestaurants)
4355
if err != nil {
4456
panic(err)
4557
}
4658
// end insertMany
4759

4860
// When you run this file, it should print:
49-
// 2 documents inserted with IDs: ObjectID("..."), ObjectID("...")
61+
// 3 documents inserted with IDs: ObjectID("..."), ObjectID("...")
5062
fmt.Printf("%d documents inserted with IDs:\n", len(result.InsertedIDs))
5163
for _, id := range result.InsertedIDs {
5264
fmt.Printf("\t%s\n", id)

source/usage-examples/insertMany.txt

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,28 @@ Example
1414

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

17-
The following example inserts two documents in the ``haikus`` collection:
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/insertMany.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 inserts three new documents to the ``restaurants``
31+
collection:
1832

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

2135
.. literalinclude:: /includes/usage-examples/code-snippets/insertMany.go
2236
:start-after: begin insertMany
2337
:end-before: end insertMany
24-
:emphasize-lines: 7
38+
:emphasize-lines: 8
2539
:language: go
2640
:dedent:
2741

@@ -31,23 +45,32 @@ Expected Result
3145
---------------
3246

3347
After you run the full example, you can find the following inserted
34-
documents in the ``haikus`` collection:
48+
documents in the ``restaurants`` collection:
3549

3650
.. code-block:: json
3751
:copyable: false
3852

3953
{
4054
"_id": ObjectId("..."),
41-
"title": "Record of a Shriveled Datum",
42-
"text": "No bytes, no problem. Inserting a document. In MongoDB"
55+
"name": "Rule of Thirds",
56+
"cuisine": "Japanese",
57+
"borough": "Brooklyn"
58+
},
59+
{
60+
"_id": ObjectId("..."),
61+
"title": "Soothr",
62+
"cuisine": "Thai",
63+
"borough": "Manhattan"
4364
},
4465
{
4566
"_id": ObjectId("..."),
46-
"title": "Showcasing a Blossoming Binary",
47-
"text": "Binary data, safely stored with GridFS. Bucket the data"
67+
"title": "Madame Vo",
68+
"cuisine": "Vietnamese",
69+
"borough": "Manhattan"
4870
}
4971

50-
For an example on how to find multiple documents, see :ref:`golang-find-multiple`.
72+
For an example on how to find multiple documents, see the
73+
:ref:`golang-find-multiple` usage example.
5174

5275
Additional Information
5376
----------------------

0 commit comments

Comments
 (0)