|
| 1 | +==================================== |
| 2 | +Limit the Number of Returned Results |
| 3 | +==================================== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +Use ``limit()`` to cap the number of documents that a read operation returns. |
| 8 | +This instance method designates the maximum number of |
| 9 | +documents that a read operation can return. If there are not enough documents |
| 10 | +to reach the specified limit, it can return a smaller number. |
| 11 | +If you use ``limit()`` with the ``skip()`` (TODO: link) instance method, the skip applies |
| 12 | +first and the limit only applies to the documents left over after |
| 13 | +the skip. |
| 14 | + |
| 15 | +The examples below demonstrate, respectively, how to insert data into |
| 16 | +a collection, how to use ``limit()`` to restrict the number of returned documents, |
| 17 | +and how to combine ``limit()`` with ``skip()`` to further narrow the results returned from a query. |
| 18 | + |
| 19 | +The following operation inserts documents representing books into a collection: |
| 20 | + |
| 21 | +.. code-block:: java |
| 22 | + |
| 23 | + collection.insertMany(Arrays.asList( |
| 24 | + new Document().append("_id", 1) |
| 25 | + .append("title", "The Brothers Karamazov").append("length", 824) |
| 26 | + .append("author", "Dostoyevsky"), |
| 27 | + new Document().append("_id", 2) |
| 28 | + .append("title", "Les Misérables").append("length", 1462).append("author", "Hugo"), |
| 29 | + new Document().append("_id", 3) |
| 30 | + .append("title", "Atlas Shrugged").append("length", 1088).append("author", "Rand"), |
| 31 | + new Document().append("_id", 4) |
| 32 | + .append("title", "Infinite Jest").append("length", 1104).append("author", "Wallace"), |
| 33 | + new Document().append("_id", 5) |
| 34 | + .append("title", "Cryptonomicon").append("length", 918).append("author", "Stephenson"), |
| 35 | + new Document().append("_id", 6) |
| 36 | + .append("title", "A Dance with Dragons").append("length", 1104) |
| 37 | + .append("author", "Martin") |
| 38 | + )); |
| 39 | + |
| 40 | +The next example queries the collection to return the top three |
| 41 | +longest books. It first matches all the documents with the query, then sorts on the |
| 42 | +``length`` field to return books with longer lengths before |
| 43 | +books with shorter lengths. Lastly, it limits the return value to ``3`` documents: |
| 44 | + |
| 45 | +.. code-block:: java |
| 46 | + :emphasize-lines: 6 |
| 47 | + |
| 48 | + import com.mongodb.client.model.Sorts; |
| 49 | + |
| 50 | + // define a cursor that will return the first 3 sorted items |
| 51 | + MongoCursor<Document> cursor = collection.find() |
| 52 | + .sort(descending("length")) |
| 53 | + .limit(3) |
| 54 | + .iterator(); |
| 55 | + // print out items |
| 56 | + try { |
| 57 | + while (cursor.hasNext()) { |
| 58 | + System.out.println(cursor.next()); |
| 59 | + } |
| 60 | + } |
| 61 | + // close the cursor |
| 62 | + finally { |
| 63 | + cursor.close(); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +The code example above prints out the following three documents, sorted by |
| 68 | +length: |
| 69 | + |
| 70 | +.. code-block:: java |
| 71 | + |
| 72 | + Document{{_id=2, title=Les Misérables, author=Hugo, length=1462}} |
| 73 | + Document{{_id=6, title=A Dance with Dragons, author=Martin, length=1104}} |
| 74 | + Document{{_id=4, title=Infinite Jest, author=Wallace, length=1104}} |
| 75 | + |
| 76 | +.. note:: |
| 77 | + |
| 78 | + The order in which you call ``limit()`` and ``sort()`` does not matter |
| 79 | + because the driver reorders the calls to apply the sort first and the |
| 80 | + limit after it. The following two calls are equivalent: |
| 81 | + |
| 82 | + .. code-block:: java |
| 83 | + |
| 84 | + collection.find().sort(descending("length")).limit(3); |
| 85 | + collection.find().limit(3).sort(descending("length")); |
| 86 | + |
| 87 | + |
| 88 | +To see the next three longest books, append the ``skip()`` method to your |
| 89 | +``find()`` call. The integer argument passed to ``skip()`` will determine |
| 90 | +how many documents the find operation returns: |
| 91 | + |
| 92 | +.. code-block:: java |
| 93 | + :emphasize-lines: 3, 4 |
| 94 | + |
| 95 | + MongoCursor<Document> cursor = collection.find() |
| 96 | + .sort(ascending("length")) |
| 97 | + .limit(3) |
| 98 | + .skip(3) |
| 99 | + .iterator(); |
| 100 | + |
| 101 | +This operation returns the documents that describe the fourth through sixth |
| 102 | +longest books: |
| 103 | + |
| 104 | +.. code-block:: java |
| 105 | + |
| 106 | + Document{{_id=3, title=Atlas Shrugged, author=Rand, length=1088}} |
| 107 | + Document{{_id=5, title=Cryptonomicon, author=Stephenson, length=918}} |
| 108 | + Document{{_id=1, title=The Brothers Karamazov, author=Dostoyevsky, length=824}} |
| 109 | + |
| 110 | +You can combine ``skip()`` and ``limit()`` in this way to implement paging for your |
| 111 | +collection, returning only small subsets of the collection at one time. |
| 112 | + |
| 113 | +.. note:: |
| 114 | + |
| 115 | + In order to ensure stable sorts across multiple queries, you must sort |
| 116 | + using a unique key (such as ``_id``). Otherwise, a call to ``skip()`` |
| 117 | + and ``limit()`` may produce unpredictable results when combined with |
| 118 | + ``sort()``. |
| 119 | + |
| 120 | + For example, consider the following data: |
| 121 | + |
| 122 | + .. code_block:: java |
| 123 | + |
| 124 | + { type: "computer", data: "1", serial_no: 235235 } |
| 125 | + { type: "computer", data: "2", serial_no: 235237 } |
| 126 | + { type: "computer", data: "3", serial_no: 235239 } |
| 127 | + { type: "computer", data: "4", serial_no: 235241 } |
| 128 | + |
| 129 | + If you sorted by ``type`` alone, ``sort()`` does not guarantee the same order |
| 130 | + upon return. Appending ``skip()`` and ``limit()`` to the ``sort()`` |
| 131 | + could return different documents for different queries. In this case, sorting |
| 132 | + by ``data`` or ``serial_no`` would guarantee a stable sort, as both are unique keys. |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +For additional information on the classes and methods mentioned on this |
| 137 | +page, see the following API documentation: |
| 138 | + |
| 139 | +- :java-sync-api:`FindIterable <com/mongodb/client/FindIterable.html>` |
| 140 | +- :java-sync-api:`MongoIterable <com/mongodb/client/MongoIterable.html>` |
| 141 | +- :java-sync-api:`MongoCursor <com/mongodb/client/MongoCursor.html>` |
| 142 | +- :java-sync-api:`find() <com/mongodb/client/MongoCollection.html#find()>` |
0 commit comments