@@ -26,10 +26,10 @@ from the existing set of data, you can use a find operation such as the
26
26
You can also further specify the information you are requesting by
27
27
including additional parameters or by chaining other methods such as:
28
28
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`
33
33
34
34
You can also use an aggregation operation to retrieve data. This type of
35
35
operation allows you to apply an ordered pipeline of transformations to the
@@ -44,11 +44,41 @@ matching data is inserted.
44
44
Find
45
45
----
46
46
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);
52
82
53
83
If you resolve the ``Promise`` returned by ``find()``, you receive
54
84
a reference to a ``Cursor`` with which you can navigate matched documents.
@@ -66,15 +96,13 @@ matching document or ``null`` if there are no matches.
66
96
:start-after: start find crud example
67
97
:end-before: end find crud example
68
98
69
-
70
99
Once the operation returns, the ``findResult`` variable references a
71
100
``Cursor``. You can print the documents retrieved using the ``forEach()``
72
101
method as shown below:
73
102
74
103
.. code-block:: javascript
75
104
76
- await cursor.forEach(console.dir);
77
-
105
+ await findResult.forEach(console.dir);
78
106
79
107
The output might resemble the following:
80
108
@@ -87,8 +115,8 @@ matching document or ``null`` if there are no matches.
87
115
...
88
116
]
89
117
90
- See the :doc :`find() </ usage-examples/ find>` and :doc :`findOne()
91
- </ usage-examples/findOne >` for fully-runnable examples.
118
+ See the :ref :`find() <node- usage-find>` and :ref :`findOne()
119
+ <node- usage-findone >` for fully-runnable examples.
92
120
93
121
Aggregate
94
122
---------
@@ -110,15 +138,13 @@ group, and arrange the result data from a collection.
110
138
:start-after: start aggregate crud example
111
139
:end-before: end aggregate crud example
112
140
113
-
114
141
Once the operation returns, the ``aggregateResult`` variable references a
115
142
``Cursor``. You can print the documents retrieved using the ``forEach()``
116
143
method as shown below:
117
144
118
145
.. code-block:: javascript
119
146
120
- await cursor.forEach(console.dir);
121
-
147
+ await aggregateResult.forEach(console.dir);
122
148
123
149
The output might resemble the following:
124
150
@@ -160,4 +186,4 @@ data whenever write operations are executed on the collection.
160
186
161
187
162
188
For a runnable example of the ``watch()`` method using the NodeJS driver, see
163
- the :doc :`change streams </ usage-examples/changeStream >` usage example.
189
+ the :ref :`change streams <node- usage-watch >` usage example.
0 commit comments