Skip to content

Commit 3bbb680

Browse files
committed
DOCSP-29805: no query criteria in find (#681)
* DOCSP-29805: find op with no/empty query document * wording fixes * replace refs while im here * DB PR fixes 1 (cherry picked from commit a4883c9) (cherry picked from commit 6a8994c) (cherry picked from commit 723cb73)
1 parent 9eee34a commit 3bbb680

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

source/fundamentals/crud/read-operations/retrieve.txt

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ from the existing set of data, you can use a find operation such as the
2626
You can also further specify the information you are requesting by
2727
including additional parameters or by chaining other methods such as:
2828

29-
- :doc:`Sort Results </fundamentals/crud/read-operations/sort>`
30-
- :doc:`Skip Returned Results </fundamentals/crud/read-operations/skip>`
31-
- :doc:`Limit the Number of Returned Results </fundamentals/crud/read-operations/limit>`
32-
- :doc:`Specify Which Fields to Return </fundamentals/crud/read-operations/project>`
29+
- :ref:`node-fundamentals-sort`
30+
- :ref:`node-fundamentals-skip`
31+
- :ref:`node-fundamentals-limit`
32+
- :ref:`node-fundamentals-project`
3333

3434
You can also use an aggregation operation to retrieve data. This type of
3535
operation allows you to apply an ordered pipeline of transformations to the
@@ -44,11 +44,41 @@ matching data is inserted.
4444
Find
4545
----
4646

47-
The ``find()`` method is called on the ``Collection`` object that
48-
references the collection you want to query. The method accepts a query
49-
document that describes the documents you want to retrieve. For more
50-
information on how to specify your query document, see our guide on
51-
how to :doc:`Specify a Query </fundamentals/crud/query-document>`.
47+
You can call the ``find()`` method on a ``Collection`` object. The
48+
method accepts a query document that describes the documents you want to
49+
retrieve. For more information on how to specify your query document,
50+
see the :ref:`node-fundamentals-query-document` guide.
51+
52+
.. tip:: No Query Criteria
53+
54+
To execute a find operation that has no query criteria, you can
55+
pass an empty query or omit the query document in your find
56+
method parameters.
57+
58+
The following operations both return all documents in the
59+
``myColl`` collection:
60+
61+
.. code-block:: javascript
62+
63+
await myColl.find(); // no query
64+
await myColl.find({}); // empty query
65+
66+
If you don't pass a query or pass an empty query
67+
to the ``findOne()`` method, the operation returns a single
68+
document from a collection.
69+
70+
You can specify options in a find operation even when you pass an
71+
empty query. For example, the following code shows how you can
72+
specify a projection as an option while executing a find operation
73+
with an empty query parameter:
74+
75+
.. code-block:: javascript
76+
77+
const options = {
78+
projection: { _id: 0, field1: 1 },
79+
};
80+
81+
const findResult = await myColl.findOne({}, options);
5282

5383
To access the results, you can optionally pass a callback in the method
5484
call or resolve the returned ``Promise`` object. See our guide on
@@ -71,15 +101,13 @@ matching document or ``null`` if there are no matches.
71101
:start-after: start find crud example
72102
:end-before: end find crud example
73103

74-
75104
Once the operation returns, the ``findResult`` variable references a
76105
``Cursor``. You can print the documents retrieved using the ``forEach()``
77106
method as shown below:
78107

79108
.. code-block:: javascript
80109

81-
await cursor.forEach(console.dir);
82-
110+
await findResult.forEach(console.dir);
83111

84112
The output might resemble the following:
85113

@@ -92,8 +120,8 @@ matching document or ``null`` if there are no matches.
92120
...
93121
]
94122

95-
See the :doc:`find() </usage-examples/find>` and :doc:`findOne()
96-
</usage-examples/findOne>` for fully-runnable examples.
123+
See the :ref:`find() <node-usage-find>` and :ref:`findOne()
124+
<node-usage-findone>` for fully-runnable examples.
97125

98126
Aggregate
99127
---------
@@ -115,15 +143,13 @@ group, and arrange the result data from a collection.
115143
:start-after: start aggregate crud example
116144
:end-before: end aggregate crud example
117145

118-
119146
Once the operation returns, the ``aggregateResult`` variable references a
120147
``Cursor``. You can print the documents retrieved using the ``forEach()``
121148
method as shown below:
122149

123150
.. code-block:: javascript
124151

125-
await cursor.forEach(console.dir);
126-
152+
await aggregateResult.forEach(console.dir);
127153

128154
The output might resemble the following:
129155

@@ -165,4 +191,4 @@ data whenever write operations are executed on the collection.
165191

166192

167193
For a runnable example of the ``watch()`` method using the NodeJS driver, see
168-
the :doc:`change streams </usage-examples/changeStream>` usage example.
194+
the :ref:`change streams <node-usage-watch>` usage example.

0 commit comments

Comments
 (0)