Skip to content

DOCSP-48707 Standardize cursor chaining methods #1073

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

Open
wants to merge 7 commits into
base: comp-cov
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion source/crud/query/count.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

.. code-block:: javascript

collection.countDocuments({}, { hint: "_id_" });
collection.countDocuments({}).hint("_id");

Check failure on line 44 in source/crud/query/count.txt

View workflow job for this annotation

GitHub Actions / TDBX Vale rules

[vale] reported by reviewdog 🐶 [MongoDB.Want] Use 'want' instead of 'wish'. Raw Output: {"message": "[MongoDB.Want] Use 'want' instead of 'wish'.", "location": {"path": "source/crud/query/count.txt", "range": {"start": {"line": 44, "column": 32}}}, "severity": "ERROR"}

Example
-------
Expand Down
10 changes: 4 additions & 6 deletions source/crud/query/retrieve.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,16 @@ see the :ref:`node-fundamentals-query-document` guide.
to the ``findOne()`` method, the operation returns a single
document from a collection.

You can specify options in a find operation even when you pass an
You can chain cursor methods to 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
specify a projection while executing a find operation
that receives an empty query parameter:

.. code-block:: javascript

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

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

For more information about projecting document fields, see the
:ref:`node-fundamentals-project` guide.
Expand Down
87 changes: 18 additions & 69 deletions source/crud/query/specify-documents-to-return.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ operation by using the following methods:
- ``limit()``: Specifies the maximum number of documents to return from a query.
- ``skip()``: Specifies the number of documents to skip before returning query results.

You can use these methods either by chaining them to your read operation or by specifying them in an
``options`` object in your call to your read operation.

.. note::

If you chain ``sort()``, ``limit()``, or ``skip()`` to a read operation, you must
specify all methods before iterating the cursor. If you specify a method after
iterating the cursor, the method you specified does not apply to the operation.

Sample Data for Examples
~~~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -56,6 +47,12 @@ that describe books into the ``myDB.books`` collection:
{ "_id": 6, "name": "A Dance With Dragons", "author": "Martin", "length": 1104 },
]);

.. note::

When you chain ``sort()``, ``limit()``, or ``skip()`` to a read operation, you must
specify all methods before iterating the cursor. If you specify a method after
iterating the cursor, the method you specified does not apply to the operation.

.. include:: /includes/access-cursor-note.rst

.. _node-fundamentals-sort:
Expand Down Expand Up @@ -187,19 +184,6 @@ length:
myColl.find(query).sort({ length: -1 }).limit(3);
myColl.find(query).limit(3).sort({ length: -1 });

You can also apply ``sort()`` and ``limit()`` by specifying them in an
``options`` object in your call to the ``find()`` method. The following two
calls are equivalent:

.. code-block:: javascript

myColl.find(query).sort({ length: -1 }).limit(3);
myColl.find(query, { sort: { length: -1 }, limit: 3 });

For more information on the ``options`` settings for the ``find()``
method, see the
`API documentation on find() <{+api+}/classes/Collection.html#find>`__.

.. _node-fundamentals-skip:

Skip
Expand Down Expand Up @@ -244,21 +228,6 @@ the fifth and sixth highest length documents:
{ "_id": 5, "title": "Cryptonomicon", "author": "Stephenson", "length": 918 }
{ "_id": 1, "title": "The Brothers Karamazov", "author": "Dostoyevsky", "length": 824 }

You can also apply ``skip()`` and ``sort()`` by specifying them in an
``options`` object in your call to the ``find()`` method. The following two
calls are equivalent:

.. code-block:: javascript

myColl.find(query).sort({ length: -1 }).skip(4);
myColl.find(query, { sort: { length: -1 }, skip: 4});

For more information on the ``options`` settings for the ``find()``
method, see the
`API documentation on find() <{+api+}/classes/Collection.html#find>`__.

.. _node-fundamentals-combine-lim-sort-skip:

Combine Limit, Sort, and Skip
-----------------------------

Expand All @@ -270,50 +239,30 @@ The following example returns documents with the ``length`` value of
``"1104"``. The results are sorted in alphabetical order, skipping the first
document and includes only the first result:

.. code-block:: javascript

// define a query to look for length value of 1104
const query = {length: "1104"};
const options = {
// sort in alphabetical (1) order by title
sort : { title: 1 },
// omit the first document
skip : 1,
// returns only the first result
limit: 1,
}
const cursor = myColl.find(query, options);
for await (const doc of cursor) {
console.dir(doc);
}

.. code-block:: json
:copyable: false

{ "_id": 4, "title": "Infinite Jest", "author": "Wallace", "length": 1104 }

.. note::

The order in which you call these methods doesn't change the documents
that are returned. The driver automatically reorders the calls to perform the
sort and skip operations first, and the limit operation afterward.

You can also limit, sort, and skip results by chaining each method to the ``find`` method.
The following example specifies the same query as the preceding example:

.. io-code-block::

.. input::
:language: javascript

myColl.find(query).sort({ title: 1 }).skip(1).limit(1);
const query = {length: "1104"};

const cursor = myColl.find(query).sort({ title: 1 }).skip(1).limit(1);

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

.. output::
:language: json
:visible: false

{ "_id": 4, "title": "Infinite Jest", "author": "Wallace", "length": 1104 }

.. note::

The order in which you call these methods doesn't change the documents
that are returned.

Additional Information
----------------------

Expand Down
Loading