|
| 1 | +.. _rust-skip-guide: |
| 2 | + |
| 3 | +===================== |
| 4 | +Skip Returned Results |
| 5 | +===================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, read operation, skip, skip results |
| 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 a read |
| 24 | +operation that skips a specified number of documents when returning results. |
| 25 | + |
| 26 | +Sample Data for Examples |
| 27 | +------------------------ |
| 28 | + |
| 29 | +The examples in this guide use the following ``Book`` struct as a model for |
| 30 | +documents in the ``books`` collection: |
| 31 | + |
| 32 | +.. literalinclude:: /includes/fundamentals/code-snippets/crud/sort.rs |
| 33 | + :start-after: start-book-struct |
| 34 | + :end-before: end-book-struct |
| 35 | + :language: rust |
| 36 | + :dedent: |
| 37 | + |
| 38 | +The following code shows how to insert sample data into the ``books`` |
| 39 | +collection: |
| 40 | + |
| 41 | +.. literalinclude:: /includes/fundamentals/code-snippets/crud/sort.rs |
| 42 | + :start-after: start-sample-data |
| 43 | + :end-before: end-sample-data |
| 44 | + :language: rust |
| 45 | + :dedent: |
| 46 | + |
| 47 | +Skip Documents |
| 48 | +-------------- |
| 49 | + |
| 50 | +You can skip results retrieved by a query, or you can skip results within an |
| 51 | +aggregation pipeline. |
| 52 | + |
| 53 | +This section describes how to skip results in the following ways: |
| 54 | + |
| 55 | +- :ref:`skip() method <rust-skip-method>`: Chain the ``skip()`` method to the |
| 56 | + ``find()`` method |
| 57 | +- :ref:`FindOptions struct <rust-findoptions-skip>`: Use the ``skip()`` option |
| 58 | + builder method to configure a ``FindOptions`` struct |
| 59 | +- :ref:`Aggregation pipleline <rust-aggregation-skip>`: Create a pipeline that uses the ``$skip`` stage |
| 60 | + |
| 61 | +If the number of skipped documents exceeds the number of matched documents for a |
| 62 | +query, then that query returns no documents. |
| 63 | + |
| 64 | +Find operations return documents in a natural order that is not sorted on any |
| 65 | +fields. To avoid skipping random documents, use the ``sort()`` method to sort |
| 66 | +documents on a field with a unique value before setting a skip option. To learn |
| 67 | +more, see the :ref:`rust-sort-guide` guide. |
| 68 | + |
| 69 | +.. _rust-skip-method: |
| 70 | + |
| 71 | +skip() Method Example |
| 72 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 73 | + |
| 74 | +To skip documents, you can chain the ``skip()`` method to the ``find()`` method. |
| 75 | +The ``skip()`` method takes an integer that specifies the number of documents to |
| 76 | +omit from the beginning of the result set. |
| 77 | + |
| 78 | +This example runs a ``find()`` operation that performs the following actions: |
| 79 | + |
| 80 | +- Sorts the results in ascending order of their ``author`` field values |
| 81 | +- Skips the first two documents |
| 82 | +- Returns the remaining documents |
| 83 | + |
| 84 | +.. io-code-block:: |
| 85 | + :copyable: true |
| 86 | + |
| 87 | + .. input:: /includes/fundamentals/code-snippets/crud/skip.rs |
| 88 | + :start-after: start-skip-example |
| 89 | + :end-before: end-skip-example |
| 90 | + :language: rust |
| 91 | + :dedent: |
| 92 | + |
| 93 | + .. output:: |
| 94 | + :language: console |
| 95 | + :visible: false |
| 96 | + |
| 97 | + Book { name: "A Dance with Dragons", author: "Martin", length: 1104 } |
| 98 | + Book { name: "Atlas Shrugged", author: "Rand", length: 1088 } |
| 99 | + |
| 100 | +.. _rust-findoptions-skip: |
| 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 ``skip`` field of the ``FindOptions`` struct by |
| 107 | +using the ``skip()`` 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 | +- Sorts the results in descending order of their ``name`` field values |
| 114 | +- Skips the first document |
| 115 | +- Returns the remaining documents |
| 116 | + |
| 117 | +.. io-code-block:: |
| 118 | + :copyable: true |
| 119 | + |
| 120 | + .. input:: /includes/fundamentals/code-snippets/crud/skip.rs |
| 121 | + :start-after: start-options-skip-example |
| 122 | + :end-before: end-options-skip-example |
| 123 | + :language: rust |
| 124 | + :dedent: |
| 125 | + |
| 126 | + .. output:: |
| 127 | + :language: console |
| 128 | + :visible: false |
| 129 | + |
| 130 | + Book { name: "Les Misérables", author: "Hugo", length: 1462 } |
| 131 | + Book { name: "Atlas Shrugged", author: "Rand", length: 1088 } |
| 132 | + |
| 133 | +.. _rust-aggregation-skip: |
| 134 | + |
| 135 | +Aggregation Example |
| 136 | +~~~~~~~~~~~~~~~~~~~ |
| 137 | + |
| 138 | +You can use the ``$skip`` stage in an aggregation pipeline to skip documents. To |
| 139 | +learn more about aggregation operations, see the :ref:`rust-aggregation` guide. |
| 140 | + |
| 141 | +This example runs an aggregation pipeline that performs the following actions: |
| 142 | + |
| 143 | +- Sorts the results in ascending order of their ``author`` field values |
| 144 | +- Skips the first document |
| 145 | +- Returns the remaining documents |
| 146 | + |
| 147 | +.. io-code-block:: |
| 148 | + :copyable: true |
| 149 | + |
| 150 | + .. input:: /includes/fundamentals/code-snippets/crud/skip.rs |
| 151 | + :start-after: start-aggregation-example |
| 152 | + :end-before: end-aggregation-example |
| 153 | + :language: rust |
| 154 | + :dedent: |
| 155 | + |
| 156 | + .. output:: |
| 157 | + :language: console |
| 158 | + :visible: false |
| 159 | + |
| 160 | + Document({"_id": Int32(3), "name": String("Les Misérables"), "author": String("Hugo"), "length": Int32(1462)}) |
| 161 | + Document({"_id": Int32(4), "name": String("A Dance with Dragons"), "author": String("Martin"), "length": Int32(1104)}) |
| 162 | + Document({"_id": Int32(2), "name": String("Atlas Shrugged"), "author": String("Rand"), "length": Int32(1088)}) |
| 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-compound-operations` |
| 172 | +- :ref:`rust-aggregation` |
| 173 | +- :ref:`rust-sort-guide` |
| 174 | +.. - :ref:`rust-limit-guide` |
| 175 | + |
| 176 | +API Documentation |
| 177 | +~~~~~~~~~~~~~~~~~ |
| 178 | + |
| 179 | +To learn more about any of the methods or types discussed in this guide, see the |
| 180 | +following API documentation: |
| 181 | + |
| 182 | +- `find() <{+api+}/struct.Collection.html#method.find>`__ |
| 183 | +- `FindOptions <{+api+}/options/struct.FindOptions.html>`__ |
| 184 | +- `FindOneOptions <{+api+}/options/struct.FindOneOptions.html>`__ |
| 185 | +- `Cursor <{+api+}/struct.Cursor.html>`__ |
| 186 | +- `aggregate() <{+api+}/struct.Collection.html#method.aggregate>`__ |
| 187 | +- `AggregateOptions <{+api+}/options/struct.AggregateOptions.html>`__ |
0 commit comments