Skip to content

DOCSP-48708 Specify cursor limitations #1094

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 6 commits into from
Apr 23, 2025
Merged
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
11 changes: 3 additions & 8 deletions source/code-snippets/usage-examples/find.js
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For technical reviewer: Can cursor chaining be used for findOne? Tried to chain for the findOne example on the page, but I got a TypeError: movies.findOne(...).sort is not a function error. It seems like findOne doesn't return a cursor but want to check anyways if it's an option to chain. Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

You are correct that findOne cannot be chained as it is immediately exhausting the cursor and returning a Promise<Document> not a cursor.

Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ async function run() {

// Query for movies that have a runtime less than 15 minutes
const query = { runtime: { $lt: 15 } };

const options = {
// Sort returned documents in ascending order by title (A->Z)
sort: { title: 1 },
// Include only the `title` and `imdb` fields in each returned document
projection: { _id: 0, title: 1, imdb: 1 },
};
const sortFields = { title: 1 };
const projectFields = { _id: 0, title: 1, imdb: 1 };

// Execute query
const cursor = movies.find(query, options);
const cursor = movies.find(query).sort(sortFields).project(projectFields);

// Print a message if no documents were found
if ((await movies.countDocuments(query)) === 0) {
Expand Down
11 changes: 4 additions & 7 deletions source/code-snippets/usage-examples/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ async function run() {
const movies = database.collection<Movie>("movies");

const query = { runtime: { $lt: 15 } };
const cursor = movies.find<Movie>(
query,
{
sort: { title: 1 },
projection: { _id: 0, title: 1, imdb: 1 },
}
);
const sortFields = { title: 1 };
const projectFields = { _id: 0, title: 1, imdb: 1 };

const cursor = movies.find<Movie>(query).sort(sortFields).project(projectFields);

if ((await movies.countDocuments(query)) === 0) {
console.warn("No documents found!");
Expand Down
14 changes: 8 additions & 6 deletions source/crud/query/project.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ field of each document:
:emphasize-lines: 2

// return only* the name field
const projection = { name: 1 };
const cursor = myColl.find().project(projection);
const projectFields = { name: 1 };
const cursor = myColl.find().project(projectFields);
for await (const doc of cursor) {
console.dir(doc);
}
Expand Down Expand Up @@ -101,8 +101,8 @@ returned documents.
:emphasize-lines: 2

// return only the name field
const projection = { _id: 0, name: 1 };
const cursor = myColl.find().project(projection);
const projectFields = { _id: 0, name: 1 };
const cursor = myColl.find().project(projectFields);
for await (const doc of cursor) {
console.dir(doc);
}
Expand Down Expand Up @@ -130,8 +130,8 @@ order in which they are returned.

.. code-block:: javascript

const projection = { _id: 0, rating: 1, name: 1 };
const cursor = myColl.find().project(projection);
const projectFields = { _id: 0, rating: 1, name: 1 };
const cursor = myColl.find().project(projectFields);
for await (const doc of cursor) {
console.dir(doc);
}
Expand All @@ -149,3 +149,5 @@ the following results:

For more projection examples, see the
:manual:`MongoDB Manual page on Project Fields to Return from Query </tutorial/project-fields-from-query-results/>`.

.. include:: /includes/crud/chain-cursor-methods.rst
13 changes: 3 additions & 10 deletions source/crud/query/retrieve.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ see the :ref:`node-fundamentals-query-document` guide.

.. code-block:: javascript

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

const findResult = await myColl.findOne().project(projection);
const findResult = await myColl.findOne().project(projectFields);

For more information about projecting document fields, see the
:ref:`node-fundamentals-project` guide.
Expand Down Expand Up @@ -235,14 +235,7 @@ Running the preceding example results in the following output:
{ title: '8', imdb: { rating: 7.8, votes: 883, id: 1592502 } }
...

You can also specify ``sort`` and ``projection`` options by chaining the
``sort()`` and ``projection`` methods to the ``find()`` method. The following
two commands are equivalent:

.. code-block:: javascript

movies.find({ runtime: { $lt: 15 } }, { sort: { title: 1 }, projection: { _id: 0, title: 1, imdb: 1 }});
movies.find({ runtime: { $lt: 15 } }).sort({ title: 1}).project({ _id: 0, title: 1, imdb: 1 });
.. include:: /includes/crud/chain-cursor-methods.rst

Additional Information
~~~~~~~~~~~~~~~~~~~~~~
Expand Down
27 changes: 11 additions & 16 deletions source/crud/query/specify-documents-to-return.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ that describe books into the ``myDB.books`` collection:
{ "_id": 6, "name": "A Dance With Dragons", "author": "Martin", "length": 1104 },
]);

.. note::

You must chain ``FindCursor`` methods such as ``sort()``, ``limit()``, or
``skip()`` to a read operation before iterating the cursor. If you specify
a ``FindCursor`` method after iterating the cursor, the setting does not
apply to the operation.
.. include:: /includes/crud/chain-cursor-methods.rst

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

Expand All @@ -79,8 +74,8 @@ lengths:
// define an empty query document
const query = {};
// sort in descending (-1) order by length
const sort = { length: -1 };
const cursor = myColl.find(query).sort(sort);
const sortFields = { length: -1 };
const cursor = myColl.find(query).sort(sortFields);
for await (const doc of cursor) {
console.dir(doc);
}
Expand Down Expand Up @@ -111,8 +106,8 @@ add more fields to the sort document:
// define an empty query document
const query = {};
// sort in ascending (1) order by length
const sort = { length: 1, author: 1 };
const cursor = myColl.find(query).sort(sort);
const sortFields = { length: 1, author: 1 };
const cursor = myColl.find(query).sort(sortFields);
for await (const doc of cursor) {
console.dir(doc);
}
Expand Down Expand Up @@ -157,9 +152,9 @@ This example performs the following actions:
// define an empty query document
const query = {};
// sort in descending (-1) order by length
const sort = { length: -1 };
const limit = 3;
const cursor = myColl.find(query).sort(sort).limit(limit);
const sortFields = { length: -1 };
const limitNum = 3;
const cursor = myColl.find(query).sort(sortFields).limit(limitNum);
for await (const doc of cursor) {
console.dir(doc);
}
Expand Down Expand Up @@ -213,9 +208,9 @@ by performing the following actions:

// define an empty query document
const query = {};
const sort = { length: -1 };
const skip = 4;
const cursor = myColl.find(query).sort(sort).skip(skip);
const sortFields = { length: -1 };
const skipNum = 4;
const cursor = myColl.find(query).sort(sortFields).skip(skipNum);
for await (const doc of cursor) {
console.dir(doc);
}
Expand Down
6 changes: 6 additions & 0 deletions source/includes/crud/chain-cursor-methods.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. note::

You must chain a cursor method such as ``sort()``, ``limit()``,
``skip()``, or ``project()`` to a read operation before iterating the cursor.
If you specify a cursor method after iterating the cursor, the setting does
not apply to the read operation.
Loading