Skip to content

Commit 0ad07ec

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 d5af5b6 commit 0ad07ec

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
@@ -24,10 +24,10 @@ from the existing set of data, you can use a find operation such as the
2424
You can also further specify the information you are requesting by
2525
including additional parameters or by chaining other methods such as:
2626

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

3232
You can also use an aggregation operation to retrieve data. This type of
3333
operation allows you to apply an ordered pipeline of transformations to the
@@ -42,11 +42,41 @@ matching data is inserted.
4242
Find
4343
----
4444

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

5181
To access the results, you can optionally pass a callback in the method
5282
call or resolve the returned ``Promise`` object. See our guide on
@@ -69,15 +99,13 @@ matching document or ``null`` if there are no matches.
6999
:start-after: start find crud example
70100
:end-before: end find crud example
71101

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

77106
.. code-block:: javascript
78107

79-
await cursor.forEach(console.dir);
80-
108+
await findResult.forEach(console.dir);
81109

82110
The output might resemble the following:
83111

@@ -90,8 +118,8 @@ matching document or ``null`` if there are no matches.
90118
...
91119
]
92120

93-
See the :doc:`find() </usage-examples/find>` and :doc:`findOne()
94-
</usage-examples/findOne>` for fully-runnable examples.
121+
See the :ref:`find() <node-usage-find>` and :ref:`findOne()
122+
<node-usage-findone>` for fully-runnable examples.
95123

96124
Aggregate
97125
---------
@@ -113,15 +141,13 @@ group, and arrange the result data from a collection.
113141
:start-after: start aggregate crud example
114142
:end-before: end aggregate crud example
115143

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

121148
.. code-block:: javascript
122149

123-
await cursor.forEach(console.dir);
124-
150+
await aggregateResult.forEach(console.dir);
125151

126152
The output might resemble the following:
127153

@@ -163,4 +189,4 @@ data whenever write operations are executed on the collection.
163189

164190

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

0 commit comments

Comments
 (0)