Skip to content

Commit b40b092

Browse files
authored
DOCSP-48707 Standardize cursor chaining methods (#1073)
* DOCSP-48707 Standardize cursor chaining methods * tech review * edits * vale check * remove mention of options object * order comment * RR review
1 parent 62c9c4c commit b40b092

File tree

3 files changed

+24
-76
lines changed

3 files changed

+24
-76
lines changed

source/crud/query/count.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ documentation for each method for more information.
5959

6060
.. code-block:: javascript
6161

62-
collection.countDocuments({}, { hint: "_id_" });
62+
collection.countDocuments({}).hint("_id");
6363

6464
countDocuments() Example: Full File
6565
-----------------------------------

source/crud/query/retrieve.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,16 @@ see the :ref:`node-fundamentals-query-document` guide.
8989
to the ``findOne()`` method, the operation returns a single
9090
document from a collection.
9191

92-
You can specify options in a find operation even when you pass an
92+
You can chain cursor methods to a find operation even when you pass an
9393
empty query. For example, the following code shows how you can
94-
specify a projection as an option while executing a find operation
94+
specify a projection while performing a find operation
9595
that receives an empty query parameter:
9696

9797
.. code-block:: javascript
9898

99-
const options = {
100-
projection: { _id: 0, field1: 1 },
101-
};
99+
const projection = { _id: 0, field1: 1 };
102100

103-
const findResult = await myColl.findOne({}, options);
101+
const findResult = await myColl.findOne().project(projection);
104102

105103
For more information about projecting document fields, see the
106104
:ref:`node-fundamentals-project` guide.

source/crud/query/specify-documents-to-return.txt

Lines changed: 19 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,6 @@ operation by using the following methods:
2727
- ``limit()``: Specifies the maximum number of documents to return from a query.
2828
- ``skip()``: Specifies the number of documents to skip before returning query results.
2929

30-
You can use these methods either by chaining them to your read operation or by specifying them in an
31-
``options`` object in your call to your read operation.
32-
33-
.. note::
34-
35-
If you chain ``sort()``, ``limit()``, or ``skip()`` to a read operation, you must
36-
specify all methods before iterating the cursor. If you specify a method after
37-
iterating the cursor, the method you specified does not apply to the operation.
38-
3930
Sample Data for Examples
4031
~~~~~~~~~~~~~~~~~~~~~~~~
4132

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

50+
.. note::
51+
52+
You must chain ``FindCursor`` methods such as ``sort()``, ``limit()``, or
53+
``skip()`` to a read operation before iterating the cursor. If you specify
54+
a ``FindCursor`` method after iterating the cursor, the setting does not
55+
apply to the operation.
56+
5957
.. include:: /includes/access-cursor-note.rst
6058

6159
.. _node-fundamentals-sort:
@@ -187,19 +185,6 @@ length:
187185
myColl.find(query).sort({ length: -1 }).limit(3);
188186
myColl.find(query).limit(3).sort({ length: -1 });
189187

190-
You can also apply ``sort()`` and ``limit()`` by specifying them in an
191-
``options`` object in your call to the ``find()`` method. The following two
192-
calls are equivalent:
193-
194-
.. code-block:: javascript
195-
196-
myColl.find(query).sort({ length: -1 }).limit(3);
197-
myColl.find(query, { sort: { length: -1 }, limit: 3 });
198-
199-
For more information on the ``options`` settings for the ``find()``
200-
method, see the
201-
`API documentation on find() <{+api+}/classes/Collection.html#find>`__.
202-
203188
.. _node-fundamentals-skip:
204189

205190
Skip
@@ -244,21 +229,6 @@ the fifth and sixth highest length documents:
244229
{ "_id": 5, "title": "Cryptonomicon", "author": "Stephenson", "length": 918 }
245230
{ "_id": 1, "title": "The Brothers Karamazov", "author": "Dostoyevsky", "length": 824 }
246231

247-
You can also apply ``skip()`` and ``sort()`` by specifying them in an
248-
``options`` object in your call to the ``find()`` method. The following two
249-
calls are equivalent:
250-
251-
.. code-block:: javascript
252-
253-
myColl.find(query).sort({ length: -1 }).skip(4);
254-
myColl.find(query, { sort: { length: -1 }, skip: 4});
255-
256-
For more information on the ``options`` settings for the ``find()``
257-
method, see the
258-
`API documentation on find() <{+api+}/classes/Collection.html#find>`__.
259-
260-
.. _node-fundamentals-combine-lim-sort-skip:
261-
262232
Combine Limit, Sort, and Skip
263233
-----------------------------
264234

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

273-
.. code-block:: javascript
274-
275-
// define a query to look for length value of 1104
276-
const query = {length: "1104"};
277-
const options = {
278-
// sort in alphabetical (1) order by title
279-
sort : { title: 1 },
280-
// omit the first document
281-
skip : 1,
282-
// returns only the first result
283-
limit: 1,
284-
}
285-
const cursor = myColl.find(query, options);
286-
for await (const doc of cursor) {
287-
console.dir(doc);
288-
}
289-
290-
.. code-block:: json
291-
:copyable: false
292-
293-
{ "_id": 4, "title": "Infinite Jest", "author": "Wallace", "length": 1104 }
294-
295-
.. note::
296-
297-
The order in which you call these methods doesn't change the documents
298-
that are returned. The driver automatically reorders the calls to perform the
299-
sort and skip operations first, and the limit operation afterward.
300-
301-
You can also limit, sort, and skip results by chaining each method to the ``find`` method.
302-
The following example specifies the same query as the preceding example:
303-
304243
.. io-code-block::
305244

306245
.. input::
307246
:language: javascript
308247

309-
myColl.find(query).sort({ title: 1 }).skip(1).limit(1);
248+
const query = {length: "1104"};
249+
250+
const cursor = myColl.find(query).sort({ title: 1 }).skip(1).limit(1);
251+
252+
for await (const doc of cursor) {
253+
console.dir(doc);
254+
}
310255

311256
.. output::
312257
:language: json
313258
:visible: false
314259

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

262+
.. note::
263+
264+
The order in which you call these methods doesn't change the documents
265+
that are returned.
266+
317267
Additional Information
318268
----------------------
319269

0 commit comments

Comments
 (0)