Skip to content

Commit 661ecbf

Browse files
author
Chris Cho
authored
DOCSP-10384: feedback usage example find many (#103)
* DOCSP-10384: address feedback for usage example - find many
1 parent 5ac11f6 commit 661ecbf

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
lines changed

source/code-snippets/usage-examples/find.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ async function run() {
1616

1717
// query for movies that have a runtime less than 15 minutes
1818
const query = { runtime: { $lt: 15 } };
19-
// sort returned documents in ascending order by title (A->Z)
20-
const sort = { title: 1 };
19+
20+
const options = {
21+
// sort returned documents in ascending order by title (A->Z)
22+
sort: { title: 1 },
2123
// Include only the `title` and `imdb` fields in each returned document
22-
const projection = { _id: 0, title: 1, imdb: 1 };
24+
projection: { _id: 0, title: 1, imdb: 1 },
25+
};
2326

24-
// find documents based on our query, sort, and projection
25-
const cursor = collection
26-
.find(query)
27-
.sort(sort)
28-
.project(projection);
27+
const cursor = collection.find(query, options);
2928

3029
// print a message if no documents were found
3130
if ((await cursor.count()) === 0) {

source/usage-examples/find.txt

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ You can also define additional query options such as
1616
:doc:`sort </fundamentals/crud/read-operations/sort>`
1717
and
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
3127
methods like ``next()``, ``toArray()``, or ``forEach()`` to
3228
fetch and work with the returned documents. If no documents match the
@@ -35,14 +31,15 @@ query, ``find()`` returns an empty cursor.
3531
Example
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 });

source/usage-examples/findOne.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ collection. It uses the following parameters:
4949
.. include:: /includes/connect-guide-note.rst
5050

5151
.. literalinclude:: /code-snippets/usage-examples/findOne.js
52-
:language: javascript
53-
:dedent: 4
52+
:language: javascript
53+
:dedent: 4
5454

5555
If you run the example above, you should see a result that resembles the
5656
following:
@@ -59,3 +59,13 @@ following:
5959

6060
{ title: 'The Room', imdb: { rating: 3.5, votes: 25673, id: 368226 } }
6161

62+
The ``sort`` and ``projection`` options can also be specified as methods
63+
(``sort()`` and ``project()``, respectively) chained to the ``findOne`` method.
64+
The following two commands are equivalent:
65+
66+
.. code-block:: javascript
67+
68+
collection.findOne(query, { sort: { rating: -1}, projection: { _id: 0, title: 1, imdb: 1});
69+
collection.findOne(query).sort({rating: -1}).project({ _id: 0, title: 1, imdb: 1});
70+
71+

0 commit comments

Comments
 (0)