|
| 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 | +This section describes how to limit results in the following ways: |
| 64 | + |
| 65 | +- :ref:`limit() method <rust-limit-method>`: Chain the ``limit()`` method to the |
| 66 | + ``find()`` method |
| 67 | +- :ref:`FindOptions struct <rust-findoptions-limit>`: Use the ``limit`` option |
| 68 | +- :ref:`Aggregation pipleline <rust-aggregation-limit>`: Create a pipeline that uses the ``$limit`` stage |
| 69 | + |
| 70 | +.. _rust-limit-method: |
| 71 | + |
| 72 | +limit() Method Example |
| 73 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 74 | + |
| 75 | +To limit the number of documents returned, you can chain the ``limit()`` method |
| 76 | +to the ``find()`` method. |
| 77 | + |
| 78 | +This example runs a ``find()`` operation that performs the following actions: |
| 79 | + |
| 80 | +- Sorts the results in ascending order of their ``length`` field values |
| 81 | +- Limits the results to the first three documents |
| 82 | + |
| 83 | +.. io-code-block:: |
| 84 | + :copyable: true |
| 85 | + |
| 86 | + .. input:: /includes/fundamentals/code-snippets/crud/limit.rs |
| 87 | + :start-after: start-limit-example |
| 88 | + :end-before: end-limit-example |
| 89 | + :language: rust |
| 90 | + :dedent: |
| 91 | + |
| 92 | + .. output:: |
| 93 | + :language: console |
| 94 | + :visible: false |
| 95 | + |
| 96 | + Book { name: "The Brothers Karamazov", author: "Dostoyevsky", length: 824 } |
| 97 | + Book { name: "Atlas Shrugged", author: "Rand", length: 1088 } |
| 98 | + Book { name: "A Dance with Dragons", author: "Martin", length: 1104 } |
| 99 | + |
| 100 | +.. _rust-findoptions-limit: |
| 101 | + |
| 102 | +Options Example |
| 103 | +~~~~~~~~~~~~~~~ |
| 104 | + |
| 105 | +Alternatively, if you are setting and reusing options for your query, you can |
| 106 | +use ``FindOptions``. Set the ``limit`` field of the ``FindOptions`` struct by |
| 107 | +using the ``limit()`` option builder method. Then, chain the ``with_options()`` |
| 108 | +method to the ``find()`` method and pass your ``FindOptions`` struct as a |
| 109 | +parameter to the ``with_options()`` method. |
| 110 | + |
| 111 | +This example runs a ``find()`` operation that performs the following actions: |
| 112 | + |
| 113 | +- Filters the results to only include documents where the ``length`` field is |
| 114 | + greater than ``1000`` |
| 115 | +- Sorts the results in ascending order of their ``length`` field values |
| 116 | +- Limits the results to the first two documents |
| 117 | + |
| 118 | +.. io-code-block:: |
| 119 | + :copyable: true |
| 120 | + |
| 121 | + .. input:: /includes/fundamentals/code-snippets/crud/limit.rs |
| 122 | + :start-after: start-limit-options-example |
| 123 | + :end-before: end-limit-options-example |
| 124 | + :language: rust |
| 125 | + :dedent: |
| 126 | + |
| 127 | + .. output:: |
| 128 | + :language: console |
| 129 | + :visible: false |
| 130 | + |
| 131 | + Book { name: "Atlas Shrugged", author: "Rand", length: 1088 } |
| 132 | + Book { name: "A Dance with Dragons", author: "Martin", length: 1104 } |
| 133 | + |
| 134 | +.. _rust-aggregation-limit: |
| 135 | + |
| 136 | +Aggregation Example |
| 137 | +~~~~~~~~~~~~~~~~~~~ |
| 138 | + |
| 139 | +You can use the ``$limit`` stage in an aggregation pipeline to limit returned |
| 140 | +results. To learn more about aggregation operations, see the |
| 141 | +:ref:`rust-aggregation` guide. |
| 142 | + |
| 143 | +This example runs an aggregation pipeline that performs the following actions: |
| 144 | + |
| 145 | +- Sorts the results in descending order of their ``length`` field values |
| 146 | +- Limits the returned results to the first two documents |
| 147 | + |
| 148 | +.. io-code-block:: |
| 149 | + :copyable: true |
| 150 | + |
| 151 | + .. input:: /includes/fundamentals/code-snippets/crud/limit.rs |
| 152 | + :start-after: start-aggregation-limit-example |
| 153 | + :end-before: end-aggregation-limit-example |
| 154 | + :language: rust |
| 155 | + :dedent: |
| 156 | + |
| 157 | + .. output:: |
| 158 | + :language: console |
| 159 | + :visible: false |
| 160 | + |
| 161 | + Document({"_id": Int32(3), "name": String("Les Misérables"), "author": String("Hugo"), "length": Int32(1462)}) |
| 162 | + Document({"_id": Int32(4), "name": String("A Dance with Dragons"), "author": String("Martin"), "length": Int32(1104)}) |
| 163 | + |
| 164 | +Additional Information |
| 165 | +---------------------- |
| 166 | + |
| 167 | +To learn more about the operations mentioned in this guide, see the following guides: |
| 168 | + |
| 169 | +- :ref:`rust-query-guide` |
| 170 | +- :ref:`rust-retrieve-guide` |
| 171 | +- :ref:`rust-aggregation` |
| 172 | +- :ref:`rust-sort-guide` |
| 173 | + |
| 174 | +API Documentation |
| 175 | +~~~~~~~~~~~~~~~~~ |
| 176 | + |
| 177 | +To learn more about any of the methods or types discussed in this guide, see the |
| 178 | +following API documentation: |
| 179 | + |
| 180 | +- `find() <{+api+}/struct.Collection.html#method.find>`__ |
| 181 | +- `FindOptions <{+api+}/options/struct.FindOptions.html>`__ |
| 182 | +- `Cursor <{+api+}/struct.Cursor.html>`__ |
| 183 | +- `aggregate() <{+api+}/struct.Collection.html#method.aggregate>`__ |
0 commit comments