@@ -59,79 +59,15 @@ Find Operations
59
59
Use **find operations** to retrieve data from MongoDB. Find operations
60
60
consist of the ``Find()`` and ``FindOne()`` methods.
61
61
62
+ .. _golang-find-example:
63
+
62
64
Find All Documents
63
65
~~~~~~~~~~~~~~~~~~
64
66
65
67
The ``Find()`` method expects you to pass a ``Context`` type and a
66
68
query filter. The method returns *all* documents that match the filter
67
69
as a ``Cursor`` type.
68
70
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
-
135
71
The following example passes a context, filter, and ``FindOptions`` to
136
72
the ``Find()`` method, which performs the following actions:
137
73
@@ -155,10 +91,16 @@ the ``Find()`` method, which performs the following actions:
155
91
{"item":"Sencha","rating":7,"date_ordered":"2009-11-18T05:00:00Z"}
156
92
{"item":"Masala","rating":8,"date_ordered":"2009-12-01T05:00:00Z"}
157
93
94
+ To learn how to access data by using a cursor, see the :ref:`golang-cursor` guide.
95
+
158
96
.. _golang-find-one-example:
159
97
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.
162
104
163
105
The following example passes a context, filter, and ``FindOneOptions``
164
106
to the ``FindOne()`` method, which performs the following actions:
@@ -182,10 +124,17 @@ to the ``FindOne()`` method, which performs the following actions:
182
124
183
125
{"item":"Masala","rating":9,"date_ordered":"2009-11-12T05:00:00Z"}
184
126
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
+
185
134
.. _golang-find-one-by-id:
186
135
187
136
Find One by ObjectId Example
188
- ----------------------------
137
+ ````````````````````````````
189
138
190
139
This example defines an ``id`` variable with a value of type ``ObjectId``
191
140
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:
232
181
about the ``_id`` field, see the :ref:`_id Field <golang-insert-id>`
233
182
section of the Insert a Document page.
234
183
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
+
235
226
.. _golang-retrieve-aggregation:
236
227
237
228
Aggregation Operations
0 commit comments