@@ -63,12 +63,57 @@ filter as a ``SingleResult`` type.
63
63
To learn how to access data in a ``SingleResult`` see the :ref:`BSON
64
64
<bson-unmarshalling>` guide.
65
65
66
+ Modify Behavior
67
+ ~~~~~~~~~~~~~~~
68
+
69
+ You can modify the behavior of ``Find()`` and ``FindOne()`` by passing
70
+ in a ``FindOptions`` and ``FindOneOptions`` type respectively. If you
71
+ don't specify any options, the driver uses the default values for each
72
+ option.
73
+
74
+ You can configure the commonly used options in both types with the
75
+ following functions:
76
+
77
+ .. list-table::
78
+ :widths: 30 70
79
+ :header-rows: 1
80
+
81
+ * - Function
82
+ - Description
83
+
84
+ * - ``SetCollation()``
85
+ - | The type of language collation to use when sorting results.
86
+ | Default: ``nil``
87
+
88
+ * - ``SetLimit()``
89
+ - | The maximum number of documents to return.
90
+ | Default: ``0``
91
+
92
+ .. note::
93
+
94
+ This option is not available for ``FindOneOptions``. The
95
+ ``FindOne()`` function internally uses ``SetLimit(-1)``.
96
+
97
+ * - ``SetProjection()``
98
+ - | The fields to include in the returned documents.
99
+ | Default: ``nil``
100
+
101
+ * - ``SetSkip()``
102
+ - | The number of documents to skip.
103
+ | Default: ``0``
104
+
105
+ * - ``SetSort()``
106
+ - | The field and type of sort to order the matched documents. You can specify an ascending or descending sort.
107
+ | Default: none
108
+
66
109
Example
67
110
```````
68
111
69
- The following example passes a context and a filter to the ``Find()``
70
- function, which matches documents where the ``ratings`` field falls
71
- between ``5`` and ``10``:
112
+ The following example passes a context, filter, and ``FindOptions`` to
113
+ the ``Find()`` function, which performs the following actions:
114
+
115
+ - Matches documents where the ``rating`` falls between ``5`` and ``10``
116
+ - Returns the ``type`` and ``rating``, but excludes the ``_id``
72
117
73
118
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/retrieve.go
74
119
:language: go
@@ -81,8 +126,31 @@ After running this example, the output resembles the following:
81
126
.. code-block:: none
82
127
:copyable: false
83
128
84
- [_id:ObjectID("...") type:Masala rating:7 ]
85
- [_id:ObjectID("...") type:Earl Grey rating:9 ]
129
+ [{type Masala} {rating 7}]
130
+ [{type Earl Grey} {rating 9}]
131
+
132
+ Example
133
+ ```````
134
+
135
+ The following example passes a context, filter, and ``FindOneOptions``
136
+ to the ``FindOne()`` function, which performs the following actions:
137
+
138
+ - Matches all documents
139
+ - A descending sort on the ``rating`` field
140
+ - Returns the ``type`` and ``rating``, but excludes the ``_id``
141
+
142
+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/retrieve.go
143
+ :language: go
144
+ :dedent:
145
+ :start-after: begin find one docs
146
+ :end-before: end find one docs
147
+
148
+ After running this example, the output resembles the following:
149
+
150
+ .. code-block:: none
151
+ :copyable: false
152
+
153
+ [{type Masala} {rating 10}]
86
154
87
155
.. _retrieve-aggregation:
88
156
@@ -108,10 +176,65 @@ stage, the pipeline proceeds using all documents in the collection.
108
176
.. To learn how to access data in a cursor, see the :doc:`Cursor
109
177
.. </fundamentals/crud/read-operations/cursor>` guide.
110
178
179
+ Modify Behavior
180
+ ~~~~~~~~~~~~~~~
181
+
182
+ The ``Aggregate()`` function optionally takes an ``AggregateOptions``
183
+ type, which represents options you can use to modify its behavior. If
184
+ you don't specify any options, the driver uses the default values for
185
+ each option.
186
+
187
+ The ``AggregateOptions`` type allows you to configure options with the
188
+ following functions:
189
+
190
+ .. list-table::
191
+ :widths: 30 70
192
+ :header-rows: 1
193
+
194
+ * - Function
195
+ - Description
196
+
197
+ * - ``SetAllowDiskUse()``
198
+ - | Whether to write to temporary files.
199
+ | Default: ``false``
200
+
201
+ * - ``SetBatchSize()``
202
+ - | The number of documents to return in each batch.
203
+ | Default: none
204
+
205
+ * - ``SetBypassDocumentValidation()``
206
+ - | Whether to allow the write to opt-out of document level validation.
207
+ | Default: ``false``
208
+
209
+ * - ``SetCollation()``
210
+ - | The type of language collation to use when sorting results.
211
+ | Default: ``nil``
212
+
213
+ * - ``SetMaxTime()``
214
+ - | The maximum amount of time in milliseconds to allow the query to run.
215
+ | Default: ``nil``
216
+
217
+ * - ``SetMaxAwaitTime()``
218
+ - | The maximum amount of time in milliseconds for the server to wait on new documents to satisfy a tailable cursor query.
219
+ | Default: ``nil``
220
+
221
+ * - ``SetComment()``
222
+ - | An arbitrary string to help trace the operation through the database profiler, currentOp and logs.
223
+ | Default: ``""``
224
+
225
+ * - ``SetHint()``
226
+ - | The index to use to scan for documents to retrieve.
227
+ | Default: ``nil``
228
+
229
+ * - ``SetLet()``
230
+ - | Specifies parameters for the aggregate expression, which improves command readability by separating the variables from the query text.
231
+ | Default: none
232
+
111
233
Example
112
234
```````
113
235
114
- The following example passes a context and an aggregation pipeline that:
236
+ The following example passes a context and an aggregation pipeline that
237
+ performs the following actions:
115
238
116
239
- Groups reviews by types
117
240
- Calculates the average rating of each type
@@ -143,11 +266,17 @@ examples:
143
266
- :doc:`Find a Document </usage-examples/findOne>`
144
267
- :doc:`Find Multiple Documents </usage-examples/find>`
145
268
146
- .. For more information on how to specify a query, see the :doc:`Specify
147
- .. a Query </fundamentals/crud/query-document>` guide.
269
+ For more information about the read operations mentioned, see the
270
+ following guides:
271
+
272
+ - :doc:`Skip Returned Results </fundamentals/crud/read-operations/skip>`
273
+ - :doc:`Sort Results </fundamentals/crud/read-operations/sort>`
274
+ - :doc:`Limit the Number of Returned Results </fundamentals/crud/read-operations/limit>`
148
275
149
- .. For more information on aggregation, see the
150
- .. :doc:`Aggregation </fundamentals/aggregation>` guide.
276
+ .. - :doc:`Specify Which Fields to Return </fundamentals/crud/read-operations/project>`
277
+ .. - :doc:`Collations </fundamentals/collations>`
278
+ .. - :doc:`Specify a Query </fundamentals/crud/query-document>` guide.
279
+ .. - :doc:`Aggregation </fundamentals/aggregation>` guide.
151
280
152
281
API Documentation
153
282
~~~~~~~~~~~~~~~~~
@@ -156,7 +285,10 @@ For more information on any of the functions or types discussed in this
156
285
guide, see the following API Documentation:
157
286
158
287
- `Find() <{+api+}/mongo#Collection.Find>`__
288
+ - `FindOptions <{+api+}/mongo/options#FindOptions>`__
289
+ - `FindOneOptions <{+api+}/mongo/options#FindOneOptions>`__
159
290
- `Cursor <{+api+}/mongo#Cursor>`__
160
291
- `FindOne() <{+api+}/mongo#Collection.FindOne>`__
161
292
- `SingleResult <{+api+}/mongo#SingleResult>`__
162
293
- `Aggregate() <{+api+}/mongo#Collection.Aggregate>`__
294
+ - `AggregateOptions <{+api+}/mongo/options#AggregateOptions>`__
0 commit comments