@@ -16,17 +16,13 @@ You can also define additional query options such as
1616:doc:`sort </fundamentals/crud/read-operations/sort>`
1717and
1818:doc:`projection </fundamentals/crud/read-operations/project>`
19- to configure the result set. You should specify these options using the
20- cursor methods
21- :node-api:`sort() <Cursor.html#sort>`
22- and
23- :node-api:`project() <Cursor.html#project>`.
24- For detailed reference documentation, see
25- :node-api:`collection.find() <Collection.html#find>`.
19+ to configure the result set. You can specify these in the options
20+ parameter in your ``find()`` method call in ``sort`` and ``projection``
21+ objects. See :node-api:`collection.find() <Collection.html#find>` for more
22+ information on the parameters you can pass to the method.
2623
27- ``find()`` returns a
28- :node-api:`Cursor <Cursor.html>`
29- that provides the results of your query. Iterate through the
24+ The ``find()`` method returns a :node-api:`Cursor <Cursor.html>` that
25+ manages the results of your query. Iterate through the
3026:doc:`cursor </fundamentals/crud/read-operations/cursor>` using cursor
3127methods like ``next()``, ``toArray()``, or ``forEach()`` to
3228fetch and work with the returned documents. If no documents match the
@@ -35,14 +31,15 @@ query, ``find()`` returns an empty cursor.
3531Example
3632-------
3733
38- The following snippet finds documents from the ``movies``
39- collection. It uses the following:
34+ The following snippet finds documents from the ``movies`` collection. It
35+ uses the following parameters :
4036
4137- A **query document** that configures the query to return only
4238 movies with a runtime of less than 15 minutes.
4339
4440- A **sort** that organizes returned documents in ascending order by
45- title (A->Z)
41+ title (alphabetical order in which "A" comes before "Z" and "1" before
42+ "9").
4643
4744- A **projection** that explicitly excludes the ``_id`` field from
4845 returned documents and explicitly includes only the ``title`` and
@@ -51,6 +48,25 @@ collection. It uses the following:
5148.. include:: /includes/connect-guide-note.rst
5249
5350.. literalinclude:: /code-snippets/usage-examples/find.js
54- :language: javascript
55- :emphasize-lines: 18
56- :linenos:
51+ :language: javascript
52+ :dedent: 4
53+
54+ If you run the example above, you should see results that resemble the
55+ following:
56+
57+ .. code-block:: javascript
58+
59+ { title: '10 Minutes', imdb: { rating: 7.9, votes: 743, id: 339976 } }
60+ { title: '3x3', imdb: { rating: 6.9, votes: 206, id: 1654725 } }
61+ { title: '7:35 in the Morning', imdb: { rating: 7.3, votes: 1555, id: 406501 } }
62+ { title: '8', imdb: { rating: 7.8, votes: 883, id: 1592502 } }
63+ ...
64+
65+ The ``sort`` and ``projection`` options can also be specified as methods
66+ (``sort()`` and ``project()``, respectively) chained to the ``findOne`` method.
67+ The following two commands are equivalent:
68+
69+ .. code-block:: javascript
70+
71+ collection.find({ runtime: { $lt: 15 } }, { sort: { title: 1 }, projection: { _id: 0, title: 1, imdb: 1 }});
72+ collection.find({ runtime: { $lt: 15 } }).sort({ title: 1}).project({ _id: 0, title: 1, imdb: 1 });
0 commit comments