Skip to content

Commit 97607a1

Browse files
authored
Merge pull request #164 from ajhuh-mdb/DOCSP-25874-struct-tagging-findone
DOCSP-25874 Add Restaurant Structs to FindOne Usage Example
2 parents 87bd121 + 91d385d commit 97607a1

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,24 @@ import (
99

1010
"github.com/joho/godotenv"
1111
"go.mongodb.org/mongo-driver/bson"
12+
"go.mongodb.org/mongo-driver/bson/primitive"
1213
"go.mongodb.org/mongo-driver/mongo"
1314
"go.mongodb.org/mongo-driver/mongo/options"
1415
)
1516

17+
// start-restaurant-struct
18+
type Restaurant struct {
19+
ID primitive.ObjectID `bson:"_id"`
20+
Name string
21+
RestaurantId string `bson:"restaurant_id"`
22+
Cuisine string
23+
Address interface{}
24+
Borough string
25+
Grades []interface{}
26+
}
27+
28+
// end-restaurant-struct
29+
1630
func main() {
1731
if err := godotenv.Load(); err != nil {
1832
log.Println("No .env file found")
@@ -34,10 +48,12 @@ func main() {
3448
}()
3549

3650
// begin findOne
37-
coll := client.Database("sample_mflix").Collection("movies")
38-
39-
var result bson.M
40-
err = coll.FindOne(context.TODO(), bson.D{{"title", "The Room"}}).Decode(&result)
51+
coll := client.Database("sample_restaurants").Collection("restaurants")
52+
filter := bson.D{{"name", "Bagels N Buns"}}
53+
54+
var result Restaurant
55+
err = coll.FindOne(context.TODO(), filter).Decode(&result)
56+
4157
if err != nil {
4258
if err == mongo.ErrNoDocuments {
4359
// This error means your query did not match any documents.

source/usage-examples/findOne.txt

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,50 @@ Example
1414

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

17-
The following example matches documents in the ``movies`` collection
18-
in which the ``title`` is "The Room", returning the first document
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/findOne.go
21+
:start-after: start-restaurant-struct
22+
:end-before: end-restaurant-struct
23+
:language: go
24+
:copyable:
25+
:dedent:
26+
27+
The following example matches documents in the ``restaurants`` collection
28+
in which the ``name`` is "Bagels N Buns", returning the first document
1929
matched:
2030

2131
.. literalinclude:: /includes/usage-examples/code-snippets/findOne.go
2232
:start-after: begin findOne
2333
:end-before: end findOne
2434
:language: go
2535
:dedent:
26-
:emphasize-lines: 4
36+
:emphasize-lines: 5
2737

2838
View a `fully runnable example <{+example+}/findOne.go>`__
2939

3040
Expected Result
3141
---------------
3242

33-
After you run the full example, it returns a ``SingleResult``
34-
object that contains the following document:
43+
Running the full example prints the following document, which is stored in the
44+
``result`` variable as a ``Restaurant`` struct:
3545

3646
.. code-block:: json
3747
:copyable: False
3848

49+
// results truncated
3950
{
40-
...
41-
"title": "The Room",
42-
"year": 2003,
43-
...
51+
"ID": "5eb3d668b31de5d588f42950",
52+
"Name": "Bagels N Buns",
53+
"RestaurantId": "40363427"
54+
"Address": [...],
55+
"Borough": "Staten Island",
56+
"Cuisine": "Delicatessen",
57+
"Grades": [...]
4458
}
4559

60+
4661
Additional Information
4762
----------------------
4863

0 commit comments

Comments
 (0)