Skip to content

Commit 2ddf53b

Browse files
(DOCSP-17336): Add query options for find and findOne (#1752)
* add query options for find * (DOCSP-17336): Add query options for find and findOne * use include for example intro * wording * add 'options' to prototype * change example to 'let' * remove old include * fix findOne output * fix broken substitutions
1 parent c44fbb5 commit 2ddf53b

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Optional. Specifies additional options for the query. These options
2+
modify query behavior and how results are returned. To see available
3+
options, see :node-api-4.0:`FindOptions </interfaces/findoptions.html>`.

source/reference/method/db.collection.find.txt

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ db.collection.find()
1313
Definition
1414
----------
1515

16-
.. method:: db.collection.find(query, projection)
16+
.. method:: db.collection.find(query, projection, options)
1717

1818

1919
.. include:: /includes/fact-mongosh-shell-method.rst
@@ -33,11 +33,13 @@ Definition
3333

3434
- Description
3535

36-
* - ``query``
36+
* - :ref:`query <method-find-query>`
3737

3838
- document
3939

40-
- Optional. Specifies selection filter using :doc:`query operators
40+
- .. _method-find-query:
41+
42+
Optional. Specifies selection filter using :doc:`query operators
4143
</reference/operator>`. To return all documents in a collection, omit
4244
this parameter or pass an empty document (``{}``).
4345

@@ -52,10 +54,14 @@ Definition
5254
Optional. Specifies the fields to return in the documents that match the
5355
query filter. To return all fields in the matching documents, omit this
5456
parameter. For details, see :ref:`find-projection`.
55-
56-
57-
5857

58+
* - :ref:`options <method-find-options>`
59+
60+
- document
61+
62+
- .. _method-find-options:
63+
64+
.. include:: /includes/find-options-description.rst
5965

6066
:returns:
6167

@@ -851,3 +857,36 @@ Available ``mongosh`` Cursor Methods
851857
- :method:`cursor.sort()`
852858
- :method:`cursor.tailable()`
853859
- :method:`cursor.toArray()`
860+
861+
Use Variables in ``let`` Option
862+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
863+
864+
You can specify query options to modify query behavior and indicate how
865+
results are returned.
866+
867+
For example, to define variables that you can access elsewhere in the
868+
``find`` method, use the ``let`` option. To filter results using a
869+
variable, you must access the variable within the :query:`$expr`
870+
operator.
871+
872+
.. include:: /includes/let-example-create-flavors.rst
873+
874+
The following example defines a ``targetFlavor`` variable in ``let`` and
875+
uses the variable to retrieve the chocolate cake flavor:
876+
877+
.. code-block:: javascript
878+
879+
db.cakeFlavors.find(
880+
{ $expr: { $eq: [ "$flavor", "$$targetFlavor" ] } },
881+
{ _id: 0 },
882+
{ let : { targetFlavor: "chocolate" }
883+
} )
884+
885+
Output:
886+
887+
.. code-block:: javascript
888+
889+
[ { flavor: 'chocolate' } ]
890+
891+
To see all available query options, see :node-api-4.0:`FindOptions
892+
</interfaces/findoptions.html>`.

source/reference/method/db.collection.findOne.txt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ db.collection.findOne()
1313
Definition
1414
----------
1515

16-
.. method:: db.collection.findOne(query, projection)
16+
.. method:: db.collection.findOne(query, projection, options)
1717

1818

1919
.. include:: /includes/fact-mongosh-shell-method.rst
@@ -55,6 +55,12 @@ Definition
5555
Omit this parameter to return all fields in the matching
5656
document. For details, see :ref:`findOne-projection`.
5757

58+
* - ``options``
59+
60+
- document
61+
62+
- .. include:: /includes/find-options-description.rst
63+
5864
:returns:
5965
One document that satisfies the criteria specified as the first
6066
argument to this method. If you specify a ``projection``
@@ -215,3 +221,36 @@ returned. You have access to the document directly:
215221

216222
print (tojson(myName));
217223
}
224+
225+
Use Variables in ``let`` Option
226+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227+
228+
You can specify query options to modify query behavior and indicate how
229+
results are returned.
230+
231+
For example, to define variables that you can access elsewhere in the
232+
``findOne`` method, use the ``let`` option. To filter results using a
233+
variable, you must access the variable within the :query:`$expr`
234+
operator.
235+
236+
.. include:: /includes/let-example-create-flavors.rst
237+
238+
The following example defines a ``targetFlavor`` variable in ``let`` and
239+
uses the variable to retrieve the chocolate cake flavor:
240+
241+
.. code-block:: javascript
242+
243+
db.cakeFlavors.findOne(
244+
{ $expr: { $eq: [ "$flavor", "$$targetFlavor" ] } },
245+
{ _id: 0 },
246+
{ let : { targetFlavor: "chocolate" }
247+
} )
248+
249+
Output:
250+
251+
.. code-block:: javascript
252+
253+
{ flavor: 'chocolate' }
254+
255+
To see all available query options, see :node-api-4.0:`FindOptions
256+
</interfaces/findoptions.html>`.

0 commit comments

Comments
 (0)