-
Notifications
You must be signed in to change notification settings - Fork 51
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
Changes from 4 commits
3872ed4
1261365
eeb95c1
83a9f44
e26cd4c
ab47f48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 sort = { title: 1 }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [s] I wonder of naming this sortOptions or sortOpts would be clearer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good suggestion, I think naming it |
||
const projection = { _id: 0, title: 1, imdb: 1 }; | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Execute query | ||
const cursor = movies.find(query, options); | ||
const cursor = movies.find(query).sort(sort).project(projection); | ||
|
||
// Print a message if no documents were found | ||
if ((await movies.countDocuments(query)) === 0) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,3 +149,9 @@ 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/>`. | ||
|
||
.. note:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [s] What do you think of making this an include instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made an include called |
||
|
||
You must chain a cursor method such as ``project()`` to a read operation | ||
before iterating the cursor. If you specify a projection after | ||
iterating the cursor, the projection does not apply to the operation. |
There was a problem hiding this comment.
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 aTypeError: 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!There was a problem hiding this comment.
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 aPromise<Document>
not a cursor.