@@ -24,10 +24,10 @@ from the existing set of data, you can use a find operation such as the
24
24
You can also further specify the information you are requesting by
25
25
including additional parameters or by chaining other methods such as:
26
26
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`
31
31
32
32
You can also use an aggregation operation to retrieve data. This type of
33
33
operation allows you to apply an ordered pipeline of transformations to the
@@ -42,11 +42,41 @@ matching data is inserted.
42
42
Find
43
43
----
44
44
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);
50
80
51
81
To access the results, you can optionally pass a callback in the method
52
82
call or resolve the returned ``Promise`` object. See our guide on
@@ -69,15 +99,13 @@ matching document or ``null`` if there are no matches.
69
99
:start-after: start find crud example
70
100
:end-before: end find crud example
71
101
72
-
73
102
Once the operation returns, the ``findResult`` variable references a
74
103
``Cursor``. You can print the documents retrieved using the ``forEach()``
75
104
method as shown below:
76
105
77
106
.. code-block:: javascript
78
107
79
- await cursor.forEach(console.dir);
80
-
108
+ await findResult.forEach(console.dir);
81
109
82
110
The output might resemble the following:
83
111
@@ -90,8 +118,8 @@ matching document or ``null`` if there are no matches.
90
118
...
91
119
]
92
120
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.
95
123
96
124
Aggregate
97
125
---------
@@ -113,15 +141,13 @@ group, and arrange the result data from a collection.
113
141
:start-after: start aggregate crud example
114
142
:end-before: end aggregate crud example
115
143
116
-
117
144
Once the operation returns, the ``aggregateResult`` variable references a
118
145
``Cursor``. You can print the documents retrieved using the ``forEach()``
119
146
method as shown below:
120
147
121
148
.. code-block:: javascript
122
149
123
- await cursor.forEach(console.dir);
124
-
150
+ await aggregateResult.forEach(console.dir);
125
151
126
152
The output might resemble the following:
127
153
@@ -163,4 +189,4 @@ data whenever write operations are executed on the collection.
163
189
164
190
165
191
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