|
| 1 | +.. _rust-limit-guide: |
| 2 | + |
| 3 | +==================================== |
| 4 | +Limit the Number of Returned Results |
| 5 | +==================================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: read operation, code example, pipeline, customize output |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the {+driver-long+} to perform **limit** |
| 24 | +operations. These operations specify the number of documents returned from a |
| 25 | +read operation. |
| 26 | + |
| 27 | +Use the ``limit()`` method to cap the number of documents that a read operation |
| 28 | +can return. The operation returns fewer documents if there are not enough |
| 29 | +documents present to reach the specified limit. |
| 30 | + |
| 31 | +If you use the ``limit()`` method with the ``skip()`` method, the skip applies |
| 32 | +first, and the limit only applies to the remaining documents. To learn more |
| 33 | +about skip operations, see the :ref:`Skip Returned Results <rust-skip-guide>` |
| 34 | +guide. |
| 35 | + |
| 36 | +Sample Data for Examples |
| 37 | +------------------------ |
| 38 | + |
| 39 | +The examples in this guide use the following ``Book`` struct as a model for |
| 40 | +documents in the ``books`` collection: |
| 41 | + |
| 42 | +.. literalinclude:: /includes/fundamentals/code-snippets/crud/limit.rs |
| 43 | + :start-after: start-book-struct |
| 44 | + :end-before: end-book-struct |
| 45 | + :language: rust |
| 46 | + :dedent: |
| 47 | + |
| 48 | +The following code shows how to insert sample data into the ``books`` |
| 49 | +collection: |
| 50 | + |
| 51 | +.. literalinclude:: /includes/fundamentals/code-snippets/crud/limit.rs |
| 52 | + :start-after: start-sample-data |
| 53 | + :end-before: end-sample-data |
| 54 | + :language: rust |
| 55 | + :dedent: |
| 56 | + |
| 57 | +Limit Documents |
| 58 | +--------------- |
| 59 | + |
| 60 | +You can specify the maximum number of documents to return in a query or in an |
| 61 | +aggregation pipeline. |
| 62 | + |
| 63 | +.. _rust-limit-method: |
| 64 | + |
| 65 | +Query Results Example |
| 66 | +~~~~~~~~~~~~~~~~~~~~~ |
| 67 | + |
| 68 | +To limit the number of documents returned, you can initialize a ``FindOptions`` |
| 69 | +instance and specify the number of documents you want to limit using the |
| 70 | +``limit()`` method. Then, pass your ``FindOptions`` struct as a parameter to the |
| 71 | +``find()`` method. |
| 72 | + |
| 73 | +This example runs a ``find()`` operation that performs the following actions: |
| 74 | + |
| 75 | +- Filters the results to only include documents where the ``length`` field is |
| 76 | + greater than ``1000`` |
| 77 | +- Sorts the results in ascending order of their ``length`` field values |
| 78 | +- Limits the results to the first two documents |
| 79 | + |
| 80 | +.. io-code-block:: |
| 81 | + :copyable: true |
| 82 | + |
| 83 | + .. input:: /includes/fundamentals/code-snippets/crud/limit.rs |
| 84 | + :start-after: start-limit-example |
| 85 | + :end-before: end-limit-example |
| 86 | + :language: rust |
| 87 | + :dedent: |
| 88 | + |
| 89 | + .. output:: |
| 90 | + :language: console |
| 91 | + :visible: false |
| 92 | + |
| 93 | + Book { name: "Atlas Shrugged", author: "Rand", length: 1088 } |
| 94 | + Book { name: "A Dance with Dragons", author: "Martin", length: 1104 } |
| 95 | + |
| 96 | +.. _rust-aggregation-limit: |
| 97 | + |
| 98 | +Aggregation Example |
| 99 | +~~~~~~~~~~~~~~~~~~~ |
| 100 | + |
| 101 | +You can use the ``$limit`` stage in an aggregation pipeline to limit returned |
| 102 | +results. To learn more about aggregation operations, see the |
| 103 | +:ref:`rust-aggregation` guide. |
| 104 | + |
| 105 | +This example runs an aggregation pipeline that performs the following actions: |
| 106 | + |
| 107 | +- Sorts the results in descending order of their ``length`` field values |
| 108 | +- Limits the returned results to the first two documents |
| 109 | + |
| 110 | +.. io-code-block:: |
| 111 | + :copyable: true |
| 112 | + |
| 113 | + .. input:: /includes/fundamentals/code-snippets/crud/limit.rs |
| 114 | + :start-after: start-aggregation-limit-example |
| 115 | + :end-before: end-aggregation-limit-example |
| 116 | + :language: rust |
| 117 | + :dedent: |
| 118 | + |
| 119 | + .. output:: |
| 120 | + :language: console |
| 121 | + :visible: false |
| 122 | + |
| 123 | + Document({"_id": ObjectId("..."), "name": String("Les Misérables"), "author": String("Hugo"), "length": Int32(1462)}) |
| 124 | + Document({"_id": ObjectId("..."), "name": String("A Dance with Dragons"), "author": String("Martin"), "length": Int32(1104)}) |
| 125 | + |
| 126 | +Additional Information |
| 127 | +---------------------- |
| 128 | + |
| 129 | +To learn more about the operations mentioned in this guide, see the following guides: |
| 130 | + |
| 131 | +- :ref:`rust-query-guide` |
| 132 | +- :ref:`rust-retrieve-guide` |
| 133 | +- :ref:`rust-aggregation` |
| 134 | +- :ref:`rust-sort-guide` |
| 135 | + |
| 136 | +API Documentation |
| 137 | +~~~~~~~~~~~~~~~~~ |
| 138 | + |
| 139 | +To learn more about any of the methods or types discussed in this guide, see the |
| 140 | +following API documentation: |
| 141 | + |
| 142 | +- `find() <{+api+}/struct.Collection.html#method.find>`__ |
| 143 | +- `FindOptions <{+api+}/options/struct.FindOptions.html>`__ |
| 144 | +- `Cursor <{+api+}/struct.Cursor.html>`__ |
| 145 | +- `aggregate() <{+api+}/struct.Collection.html#method.aggregate>`__ |
0 commit comments