Skip to content

Commit 42e3bba

Browse files
authored
DOCSP-26412 Update "Insert One" Usage Example w/ Document Structs (#193)
* first draft: edit code sample, page, and add ref tag to struct tag section * fix capitalization * fix typo in struct tags * more typo fixes + sentence fixes * move omit empty note * address review comments
1 parent b0c03e5 commit 42e3bba

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

source/fundamentals/bson.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ than 100:
4949
To learn more about how the Go Driver handles BSON data, see the
5050
`bson package API documentation <{+api+}/bson>`__.
5151

52+
.. _golang-struct-tags:
53+
5254
Struct Tags
5355
-----------
5456

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

Lines changed: 15 additions & 4 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,10 +44,10 @@ func main() {
3344
}()
3445

3546
// begin insertOne
36-
coll := client.Database("insertDB").Collection("haikus")
37-
doc := bson.D{{"title", "Record of a Shriveled Datum"}, {"text", "No bytes, no problem. Just insert a document, in MongoDB"}}
47+
coll := client.Database("sample_restaurants").Collection("restaurants")
48+
newRestaurant := Restaurant{Name: "8282", Cuisine: "Korean", Borough: "Manhattan"}
3849

39-
result, err := coll.InsertOne(context.TODO(), doc)
50+
result, err := coll.InsertOne(context.TODO(), newRestaurant)
4051
if err != nil {
4152
panic(err)
4253
}

source/usage-examples/insertOne.txt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,22 @@ Example
1414

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

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

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

@@ -31,18 +46,20 @@ Expected Result
3146
---------------
3247

3348
After you run the full example, you can find the following inserted
34-
document in the ``haikus`` collection:
49+
document in the ``restaurants`` collection:
3550

3651
.. code-block:: json
3752
:copyable: false
3853

3954
{
4055
"_id": ObjectId("..."),
41-
"title": "Record of a Shriveled Datum",
42-
"text": "No bytes, no problem. Inserting a document. In MongoDB"
56+
"name": "8282",
57+
"cuisine": "Korean",
58+
"borough": "Manhattan"
4359
}
4460

45-
For an example on how to find a document, see :ref:`golang-find-one`.
61+
For an example on how to find a document, see the :ref:`golang-find-one` usage
62+
example.
4663

4764
Additional Information
4865
----------------------

0 commit comments

Comments
 (0)