Skip to content

Commit db2c16e

Browse files
lindseymoorerachel-mack
authored andcommitted
DOCSP-51815 Move and standardize find usage exs (mongodb#539)
* DOCSP-51815 Move and standardize find usage exs * small fixes * fix * MB review * tech feedback
1 parent 3cab7ed commit db2c16e

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

source/crud/query/retrieve.txt

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ consist of the ``Find()`` and ``FindOne()`` methods.
6262

6363
.. _golang-find-example:
6464

65-
Find All Documents
66-
~~~~~~~~~~~~~~~~~~
65+
Find Multiple Documents
66+
~~~~~~~~~~~~~~~~~~~~~~~
6767

6868
The ``Find()`` method expects you to pass a ``Context`` type and a
6969
query filter. The method returns *all* documents that match the filter
@@ -94,6 +94,33 @@ the ``Find()`` method, which performs the following actions:
9494

9595
To learn how to access data by using a cursor, see the :ref:`golang-cursor` guide.
9696

97+
Find Multiple Documents Example: Full File
98+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99+
100+
.. include:: /includes/usage-examples/example-intro.rst
101+
102+
The following example finds all documents in the ``restaurants`` collection
103+
in which the value of ``cuisine`` is ``"Italian"``. The example returns a cursor that
104+
references the matched documents and unpacks the documents into a slice:
105+
106+
.. io-code-block::
107+
:copyable: true
108+
109+
.. input:: /includes/usage-examples/code-snippets/find.go
110+
:language: go
111+
:dedent:
112+
113+
.. output::
114+
:language: none
115+
:visible: false
116+
117+
// results truncated
118+
...
119+
{ ... , "Name" : "Epistrophy Cafe", "RestaurantId": "41117553", "Cuisine" : "Italian", ... },
120+
{ ... , "Name" : "Remi", "RestaurantId": "41118090", "Cuisine" : "Italian", ... },
121+
{ ... , "Name" : "Sant Ambroeus", "RestaurantId": "41120682", "Cuisine" : "Italian", ... },
122+
...
123+
97124
.. _golang-find-one-example:
98125

99126
Find One Document
@@ -182,6 +209,36 @@ as parameters to the ``FindOne()`` method to perform the following actions:
182209
about the ``_id`` field, see the :ref:`_id Field <golang-insert-id>`
183210
section of the Insert a Document page.
184211

212+
Find One Document Example: Full File
213+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
214+
215+
.. include:: /includes/usage-examples/example-intro.rst
216+
217+
The following example finds and returns the first document in the
218+
``restaurants`` collection in which the value of ``name`` is ``"Bagels N Buns"``:
219+
220+
.. io-code-block::
221+
:copyable: true
222+
223+
.. input:: /includes/usage-examples/code-snippets/findOne.go
224+
:language: go
225+
:dedent:
226+
227+
.. output::
228+
:language: none
229+
:visible: false
230+
231+
// results truncated
232+
{
233+
"ID": "5eb3d668b31de5d588f42950",
234+
"Name": "Bagels N Buns",
235+
"RestaurantId": "40363427"
236+
"Address": [...],
237+
"Borough": "Staten Island",
238+
"Cuisine": "Delicatessen",
239+
"Grades": [...]
240+
}
241+
185242
.. _golang-retrieve-options:
186243

187244
Modify Behavior
@@ -330,20 +387,12 @@ the MongoDB server manual page on :manual:`Aggregation
330387
Additional Information
331388
----------------------
332389

333-
For runnable examples of the find operations, see the following usage
334-
examples:
335-
336-
- :ref:`golang-find-one`
337-
- :ref:`golang-find-multiple`
338-
339390
To learn more about the operations mentioned, see the following
340391
guides:
341392

342393
- :ref:`golang-query-document`
343394
- :ref:`golang-cursor`
344-
- :ref:`golang-skip`
345-
- :ref:`golang-sort-results`
346-
- :ref:`golang-limit`
395+
- :ref:`Specify Documents to Return <golang-specify-documents-to-return>`
347396
- :ref:`golang-project`
348397
- :ref:`golang-aggregation`
349398
- :ref:`golang-collations`

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"go.mongodb.org/mongo-driver/v2/mongo/options"
1515
)
1616

17-
// start-restaurant-struct
17+
// Creates a Restaurant struct as a model for documents in the restaurants collection
1818
type Restaurant struct {
1919
ID bson.ObjectID `bson:"_id"`
2020
Name string
@@ -25,8 +25,6 @@ type Restaurant struct {
2525
Grades interface{}
2626
}
2727

28-
// end-restaurant-struct
29-
3028
func main() {
3129
if err := godotenv.Load(); err != nil {
3230
log.Println("No .env file found")
@@ -47,7 +45,6 @@ func main() {
4745
}
4846
}()
4947

50-
// begin find
5148
coll := client.Database("sample_restaurants").Collection("restaurants")
5249

5350
// Creates a query filter to match documents in which the "cuisine"
@@ -65,11 +62,9 @@ func main() {
6562
if err = cursor.All(context.TODO(), &results); err != nil {
6663
panic(err)
6764
}
68-
// end find
6965

7066
// Prints the results of the find operation as structs
7167
for _, result := range results {
72-
cursor.Decode(&result)
7368
output, err := json.MarshalIndent(result, "", " ")
7469
if err != nil {
7570
panic(err)

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"go.mongodb.org/mongo-driver/v2/mongo/options"
1515
)
1616

17-
// start-restaurant-struct
17+
// Creates a Restaurant struct as a model for documents in the restaurants collection
1818
type Restaurant struct {
1919
ID bson.ObjectID `bson:"_id"`
2020
Name string
@@ -25,8 +25,6 @@ type Restaurant struct {
2525
Grades []interface{}
2626
}
2727

28-
// end-restaurant-struct
29-
3028
func main() {
3129
if err := godotenv.Load(); err != nil {
3230
log.Println("No .env file found")
@@ -47,7 +45,6 @@ func main() {
4745
}
4846
}()
4947

50-
// begin findOne
5148
coll := client.Database("sample_restaurants").Collection("restaurants")
5249

5350
// Creates a query filter to match documents in which the "name" is
@@ -66,7 +63,6 @@ func main() {
6663
}
6764
panic(err)
6865
}
69-
// end findOne
7066

7167
output, err := json.MarshalIndent(result, "", " ")
7268
if err != nil {

0 commit comments

Comments
 (0)