@@ -66,7 +66,9 @@ The ``Find()`` method expects you to pass a ``Context`` type and a
6666query filter. The method returns *all* documents that match the filter
6767as a ``Cursor`` type.
6868
69- To learn how to access data in a cursor, see :ref:`golang-cursor`.
69+ For an example that uses the ``Find()`` method, see the :ref:`golang-find-example`
70+ section of this page. To learn how to access data by using a cursor, see
71+ the :ref:`golang-cursor` guide.
7072
7173Find One Document
7274~~~~~~~~~~~~~~~~~
@@ -75,7 +77,13 @@ The ``FindOne()`` method expects you to pass a ``Context`` type and a
7577query filter. The method returns *the first document* that matches the
7678filter as a ``SingleResult`` type.
7779
78- To learn how to access data in a ``SingleResult`` see :ref:`golang-bson-unmarshalling`.
80+ For an example that uses the ``FindOne()`` method, see the
81+ :ref:`golang-find-one-example` section of this page. For an example that
82+ uses ``FindOne()`` and queries by using a specific ``ObjectId`` value, see
83+ the :ref:`golang-find-one-by-id` section of this page.
84+
85+ To learn how to access data from a ``SingleResult`` type, see
86+ :ref:`golang-bson-unmarshalling` in the BSON guide.
7987
8088.. _golang-retrieve-options:
8189
@@ -122,6 +130,8 @@ following methods:
122130 - | The field and type of sort to order the matched documents. You can specify an ascending or descending sort.
123131 | Default: none
124132
133+ .. _golang-find-example:
134+
125135Find Example
126136````````````
127137
@@ -131,44 +141,25 @@ the ``Find()`` method, which performs the following actions:
131141- Matches documents where the ``rating`` value is between ``5`` and
132142 ``9`` (exclusive)
133143- Sorts matched documents in ascending order by ``date_ordered``
134-
144+
135145.. io-code-block::
136- :copyable: true
146+ :copyable: true
137147
138- .. input::
148+ .. input:: /includes/fundamentals/code-snippets/CRUD/retrieve.go
149+ :start-after: begin find docs
150+ :end-before: end find docs
139151 :language: go
152+ :dedent:
140153
141- filter := bson.D{
142- {"$and",
143- bson.A{
144- bson.D{{"rating", bson.D{{"$gt", 5}}}},
145- bson.D{{"rating", bson.D{{"$lt", 9}}}},
146- }},
147- }
148- sort := bson.D{{"date_ordered", 1}}
149- opts := options.Find().SetSort(sort)
150-
151- cursor, err := coll.Find(context.TODO(), filter, opts)
152- if err != nil {
153- panic(err)
154- }
155-
156- var results []Review
157- if err = cursor.All(context.TODO(), &results); err != nil {
158- panic(err)
159- }
160- for _, result := range results {
161- res, _ := json.Marshal(result)
162- fmt.Println(string(res))
163- }
164-
165- .. output::
154+ .. output::
166155 :language: none
167156 :visible: false
168157
169158 {"Item":"Sencha","Rating":7,"DateOrdered":"2009-11-18T05:00:00Z"}
170159 {"Item":"Masala","Rating":8,"DateOrdered":"2009-12-01T05:00:00Z"}
171160
161+ .. _golang-find-one-example:
162+
172163Find One Example
173164````````````````
174165
@@ -180,29 +171,70 @@ to the ``FindOne()`` method, which performs the following actions:
180171- Skips the first two matched documents
181172
182173.. io-code-block::
183- :copyable: true
174+ :copyable: true
184175
185- .. input::
176+ .. input:: /includes/fundamentals/code-snippets/CRUD/retrieve.go
177+ :start-after: begin find one docs
178+ :end-before: end find one docs
186179 :language: go
180+ :dedent:
187181
188- filter := bson.D{{"date_ordered", bson.D{{"$lte", time.Date(2009, 11, 30, 0, 0, 0, 0, time.Local)}}}}
189- opts := options.FindOne().SetSkip(2)
190-
191- var result Review
192- err := coll.FindOne(context.TODO(), filter, opts).Decode(&result)
193- if err != nil {
194- panic(err)
195- }
196-
197- res, _ := json.Marshal(result)
198- fmt.Println(string(res))
199-
200- .. output::
182+ .. output::
201183 :language: none
202184 :visible: false
203185
204186 {"Item":"Masala","Rating":9,"DateOrdered":"2009-11-12T05:00:00Z"}
205187
188+ .. _golang-find-one-by-id:
189+
190+ Find One by ObjectId Example
191+ ````````````````````````````
192+
193+ This example defines an ``id`` variable with a value of type ``ObjectId``
194+ and uses ``id`` to specify a query filter. The filter matches a document
195+ with an ``_id`` field value that corresponds to the ``id`` variable.
196+ This example queries for the following document based on its ``_id`` value:
197+
198+ .. code-block:: json
199+ :copyable: false
200+ :emphasize-lines: 2
201+
202+ {
203+ _id: ObjectId('65170b42b99efdd0b07d42de'),
204+ item: "Hibiscus",
205+ rating : 4,
206+ date_ordered : 2009-12-18T05:00:00.000+00:00
207+ }
208+
209+ The following code passes the filter and a ``FindOneOptions`` instance
210+ as parameters to the ``FindOne()`` method to perform the following actions:
211+
212+ - Match the document with the specified ``ObjectId`` value
213+ - Project only the ``Item`` and ``Rating`` fields of the matched document
214+
215+ .. io-code-block::
216+ :copyable: true
217+
218+ .. input:: /includes/fundamentals/code-snippets/CRUD/retrieve.go
219+ :start-after: begin objectid
220+ :end-before: end objectid
221+ :language: go
222+ :dedent:
223+
224+ .. output::
225+ :language: none
226+ :visible: false
227+
228+ {"item":"Hibiscus","rating":4}
229+
230+ .. note::
231+
232+ The {+driver-short+} automatically generates a unique ``ObjectId``
233+ value for each document's ``_id`` field, so your ``ObjectId`` value
234+ might differ from the preceding code example. For more information
235+ about the ``_id`` field, see the :ref:`_id Field <golang-insert-id>`
236+ section of the Insert a Document page.
237+
206238.. _golang-retrieve-aggregation:
207239
208240Aggregation Operations
@@ -290,33 +322,15 @@ performs the following actions:
290322- Calculates the average rating for each item
291323
292324.. io-code-block::
293- :copyable: true
325+ :copyable: true
294326
295- .. input::
327+ .. input:: /includes/fundamentals/code-snippets/CRUD/retrieve.go
328+ :start-after: begin aggregate docs
329+ :end-before: end aggregate docs
296330 :language: go
331+ :dedent:
297332
298- groupStage := bson.D{
299- {"$group", bson.D{
300- {"_id", "$item"},
301- {"average", bson.D{
302- {"$avg", "$rating"},
303- }},
304- }}}
305-
306- cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{groupStage})
307- if err != nil {
308- panic(err)
309- }
310-
311- var results []bson.M
312- if err = cursor.All(context.TODO(), &results); err != nil {
313- panic(err)
314- }
315- for _, result := range results {
316- fmt.Printf("%v had an average rating of %v \n", result["_id"], result["average"])
317- }
318-
319- .. output::
333+ .. output::
320334 :language: none
321335 :visible: false
322336
0 commit comments