@@ -27,15 +27,6 @@ operation by using the following methods:
27
27
- ``limit()``: Specifies the maximum number of documents to return from a query.
28
28
- ``skip()``: Specifies the number of documents to skip before returning query results.
29
29
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
-
39
30
Sample Data for Examples
40
31
~~~~~~~~~~~~~~~~~~~~~~~~
41
32
@@ -56,6 +47,13 @@ that describe books into the ``myDB.books`` collection:
56
47
{ "_id": 6, "name": "A Dance With Dragons", "author": "Martin", "length": 1104 },
57
48
]);
58
49
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
+
59
57
.. include:: /includes/access-cursor-note.rst
60
58
61
59
.. _node-fundamentals-sort:
@@ -187,19 +185,6 @@ length:
187
185
myColl.find(query).sort({ length: -1 }).limit(3);
188
186
myColl.find(query).limit(3).sort({ length: -1 });
189
187
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
-
203
188
.. _node-fundamentals-skip:
204
189
205
190
Skip
@@ -244,21 +229,6 @@ the fifth and sixth highest length documents:
244
229
{ "_id": 5, "title": "Cryptonomicon", "author": "Stephenson", "length": 918 }
245
230
{ "_id": 1, "title": "The Brothers Karamazov", "author": "Dostoyevsky", "length": 824 }
246
231
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
-
262
232
Combine Limit, Sort, and Skip
263
233
-----------------------------
264
234
@@ -270,50 +240,30 @@ The following example returns documents with the ``length`` value of
270
240
``"1104"``. The results are sorted in alphabetical order, skipping the first
271
241
document and includes only the first result:
272
242
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
-
304
243
.. io-code-block::
305
244
306
245
.. input::
307
246
:language: javascript
308
247
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
+ }
310
255
311
256
.. output::
312
257
:language: json
313
258
:visible: false
314
259
315
260
{ "_id": 4, "title": "Infinite Jest", "author": "Wallace", "length": 1104 }
316
261
262
+ .. note::
263
+
264
+ The order in which you call these methods doesn't change the documents
265
+ that are returned.
266
+
317
267
Additional Information
318
268
----------------------
319
269
0 commit comments