Skip to content

Commit 089fbf1

Browse files
committed
retrieve ex fixes
1 parent 8ad9f9d commit 089fbf1

File tree

1 file changed

+60
-69
lines changed

1 file changed

+60
-69
lines changed

source/crud/query/retrieve.txt

Lines changed: 60 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -59,79 +59,15 @@ Find Operations
5959
Use **find operations** to retrieve data from MongoDB. Find operations
6060
consist of the ``Find()`` and ``FindOne()`` methods.
6161

62+
.. _golang-find-example:
63+
6264
Find All Documents
6365
~~~~~~~~~~~~~~~~~~
6466

6567
The ``Find()`` method expects you to pass a ``Context`` type and a
6668
query filter. The method returns *all* documents that match the filter
6769
as a ``Cursor`` type.
6870

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.
72-
73-
Find One Document
74-
~~~~~~~~~~~~~~~~~
75-
76-
The ``FindOne()`` method expects you to pass a ``Context`` type and a
77-
query filter. The method returns *the first document* that matches the
78-
filter as a ``SingleResult`` type.
79-
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.
87-
88-
.. _golang-retrieve-options:
89-
90-
Modify Behavior
91-
~~~~~~~~~~~~~~~
92-
93-
You can modify the behavior of ``Find()`` and ``FindOne()`` by passing
94-
in a ``FindOptions`` and ``FindOneOptions`` type respectively. If you
95-
don't specify any options, the driver uses the default values for each
96-
option.
97-
98-
You can configure the commonly used options in both types with the
99-
following methods:
100-
101-
.. list-table::
102-
:widths: 30 70
103-
:header-rows: 1
104-
105-
* - Method
106-
- Description
107-
108-
* - ``SetCollation()``
109-
- | The type of language collation to use when sorting results.
110-
| Default: ``nil``
111-
112-
* - ``SetLimit()``
113-
- | The maximum number of documents to return.
114-
| Default: ``0``
115-
| This option is not available for ``FindOneOptions``. The
116-
``FindOne()`` method internally uses ``SetLimit(-1)``.
117-
118-
* - ``SetProjection()``
119-
- | The fields to include in the returned documents.
120-
| Default: ``nil``
121-
122-
* - ``SetSkip()``
123-
- | The number of documents to skip.
124-
| Default: ``0``
125-
126-
* - ``SetSort()``
127-
- | The field and type of sort to order the matched documents. You can specify an ascending or descending sort.
128-
| Default: none
129-
130-
.. _golang-find-example:
131-
132-
Find Example
133-
------------
134-
13571
The following example passes a context, filter, and ``FindOptions`` to
13672
the ``Find()`` method, which performs the following actions:
13773

@@ -155,10 +91,16 @@ the ``Find()`` method, which performs the following actions:
15591
{"item":"Sencha","rating":7,"date_ordered":"2009-11-18T05:00:00Z"}
15692
{"item":"Masala","rating":8,"date_ordered":"2009-12-01T05:00:00Z"}
15793

94+
To learn how to access data by using a cursor, see the :ref:`golang-cursor` guide.
95+
15896
.. _golang-find-one-example:
15997

160-
Find One Example
161-
----------------
98+
Find One Document
99+
~~~~~~~~~~~~~~~~~
100+
101+
The ``FindOne()`` method expects you to pass a ``Context`` type and a
102+
query filter. The method returns *the first document* that matches the
103+
filter as a ``SingleResult`` type.
162104

163105
The following example passes a context, filter, and ``FindOneOptions``
164106
to the ``FindOne()`` method, which performs the following actions:
@@ -182,10 +124,17 @@ to the ``FindOne()`` method, which performs the following actions:
182124

183125
{"item":"Masala","rating":9,"date_ordered":"2009-11-12T05:00:00Z"}
184126

127+
For an example that
128+
uses ``FindOne()`` and queries by using a specific ``ObjectId`` value, see
129+
the :ref:`golang-find-one-by-id` section of this page.
130+
131+
To learn how to access data from a ``SingleResult`` type, see
132+
:ref:`golang-bson-unmarshalling` in the BSON guide.
133+
185134
.. _golang-find-one-by-id:
186135

187136
Find One by ObjectId Example
188-
----------------------------
137+
````````````````````````````
189138

190139
This example defines an ``id`` variable with a value of type ``ObjectId``
191140
and uses ``id`` to specify a query filter. The filter matches a document
@@ -232,6 +181,48 @@ as parameters to the ``FindOne()`` method to perform the following actions:
232181
about the ``_id`` field, see the :ref:`_id Field <golang-insert-id>`
233182
section of the Insert a Document page.
234183

184+
.. _golang-retrieve-options:
185+
186+
Modify Behavior
187+
~~~~~~~~~~~~~~~
188+
189+
You can modify the behavior of ``Find()`` and ``FindOne()`` by passing
190+
in a ``FindOptions`` and ``FindOneOptions`` type respectively. If you
191+
don't specify any options, the driver uses the default values for each
192+
option.
193+
194+
You can configure the commonly used options in both types with the
195+
following methods:
196+
197+
.. list-table::
198+
:widths: 30 70
199+
:header-rows: 1
200+
201+
* - Method
202+
- Description
203+
204+
* - ``SetCollation()``
205+
- | The type of language collation to use when sorting results.
206+
| Default: ``nil``
207+
208+
* - ``SetLimit()``
209+
- | The maximum number of documents to return.
210+
| Default: ``0``
211+
| This option is not available for ``FindOneOptions``. The
212+
``FindOne()`` method internally uses ``SetLimit(-1)``.
213+
214+
* - ``SetProjection()``
215+
- | The fields to include in the returned documents.
216+
| Default: ``nil``
217+
218+
* - ``SetSkip()``
219+
- | The number of documents to skip.
220+
| Default: ``0``
221+
222+
* - ``SetSort()``
223+
- | The field and type of sort to order the matched documents. You can specify an ascending or descending sort.
224+
| Default: none
225+
235226
.. _golang-retrieve-aggregation:
236227

237228
Aggregation Operations

0 commit comments

Comments
 (0)