Skip to content

DOCSP-29805: no query criteria in find #681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 12, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 46 additions & 18 deletions source/fundamentals/crud/read-operations/retrieve.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ from the existing set of data, you can use a find operation such as the
You can also further specify the information you are requesting by
including additional parameters or by chaining other methods such as:

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

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

The ``find()`` method is called on the ``Collection`` object that
references the collection you want to query. The method accepts a query
document that describes the documents you want to retrieve. For more
information on how to specify your query document, see our guide on
how to :doc:`Specify a Query </fundamentals/crud/query-document>`.
You can call the ``find()`` method on a ``Collection`` object. The
method accepts a query document that describes the documents you want to
retrieve. For more information on how to specify your query document,
see our guide on how to :ref:`node-fundamentals-query-document`.

.. tip:: No Query Criteria

To execute a find operation that has no query criteria, you can
pass an empty query or omit the query document in your find
method parameters.

The following operations both return all documents in the
``myColl`` collection:

.. code-block:: javascript

await myColl.find(); // no query
await myColl.find({}); // empty query

If you don't pass a query or pass an empty query
to the ``findOne()`` method, the operation returns the first
document of a collection.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question]

Is this accurate? I thought it returned a random one, not a guaranteed first one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing, it is always the first one, but here's what the Manual says:
Returns one document that satisfies the specified query criteria on the collection or view. If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk.

Maybe it's best to be on the safe side and just say a single document


Even if you pass an empty query in a find operation, you can
specify options. For example, the following code shows how you can
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[suggestion]

I'd recommend switching this to put the imperative of what readers can do before the conditional.

Suggested change
Even if you pass an empty query in a find operation, you can
specify options. For example, the following code shows how you can
You can
specify options in a find operation even when you pass an empty query. For example, the following code shows how you can

specify a projection as an option while executing a find operation
with an empty query parameter:

.. code-block:: javascript

const options = {
projection: { _id: 0, field1: 1 },
};

const findResult = await myColl.findOne({}, options);

If you resolve the ``Promise`` returned by ``find()``, you receive
a reference to a ``Cursor`` with which you can navigate matched documents.
Expand All @@ -66,15 +96,14 @@ matching document or ``null`` if there are no matches.
:start-after: start find crud example
:end-before: end find crud example


Once the operation returns, the ``findResult`` variable references a
``Cursor``. You can print the documents retrieved using the ``for await...of``
syntax as shown below:

.. code-block:: javascript

for await (const doc of cursor) {
console.dir(doc);
for await (const doc of findResult) {
console.log(doc);
}

The output might resemble the following:
Expand All @@ -88,8 +117,8 @@ matching document or ``null`` if there are no matches.
...
]

See the :doc:`find() </usage-examples/find>` and :doc:`findOne()
</usage-examples/findOne>` for fully-runnable examples.
See the :ref:`find() <node-usage-find>` and :ref:`findOne()
<node-usage-findone>` for fully-runnable examples.

Aggregate
---------
Expand All @@ -111,15 +140,14 @@ group, and arrange the result data from a collection.
:start-after: start aggregate crud example
:end-before: end aggregate crud example


Once the operation returns, the ``aggregateResult`` variable references a
``Cursor``. You can print the documents retrieved using the ``for await...of``
syntax as shown below:

.. code-block:: javascript

for await (const doc of cursor) {
console.dir(doc);
for await (const doc of aggregateResult) {
console.log(doc);
}

The output might resemble the following:
Expand Down Expand Up @@ -162,4 +190,4 @@ data whenever write operations are executed on the collection.


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