|
| 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/skip.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/skip.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 | +If the number of skipped documents exceeds the number of matched documents for a |
| 54 | +query, then that query returns no documents. |
| 55 | + |
| 56 | +Find operations return documents in a natural order that is not sorted on any |
| 57 | +fields. To avoid skipping random documents, use the ``sort()`` method to sort |
| 58 | +documents on a field with a unique value before setting a skip option. To learn |
| 59 | +more, see the :ref:`rust-sort-guide` guide. |
| 60 | + |
| 61 | +.. _rust-skip-example: |
| 62 | + |
| 63 | +Query Results Example |
| 64 | +~~~~~~~~~~~~~~~~~~~~~ |
| 65 | + |
| 66 | +To skip documents, you can initialize a ``FindOptions`` instance and specify the |
| 67 | +number of documents you want to skip using the ``skip()`` method. Then, pass |
| 68 | +your ``FindOptions`` struct as a parameter to the ``find()`` method. |
| 69 | + |
| 70 | +This example runs a ``find()`` operation that performs the following actions: |
| 71 | + |
| 72 | +- Sorts the results in ascending order of their ``author`` field values |
| 73 | +- Skips the first two documents |
| 74 | +- Returns the remaining documents |
| 75 | + |
| 76 | +.. io-code-block:: |
| 77 | + :copyable: true |
| 78 | + |
| 79 | + .. input:: /includes/fundamentals/code-snippets/crud/skip.rs |
| 80 | + :start-after: start-skip-example |
| 81 | + :end-before: end-skip-example |
| 82 | + :language: rust |
| 83 | + :dedent: |
| 84 | + |
| 85 | + .. output:: |
| 86 | + :language: console |
| 87 | + :visible: false |
| 88 | + |
| 89 | + Book { name: "A Dance with Dragons", author: "Martin", length: 1104 } |
| 90 | + Book { name: "Atlas Shrugged", author: "Rand", length: 1088 } |
| 91 | + |
| 92 | +.. _rust-aggregation-skip: |
| 93 | + |
| 94 | +Aggregation Example |
| 95 | +~~~~~~~~~~~~~~~~~~~ |
| 96 | + |
| 97 | +You can use the ``$skip`` stage in an aggregation pipeline to skip documents. To |
| 98 | +learn more about aggregation operations, see the :ref:`rust-aggregation` guide. |
| 99 | + |
| 100 | +This example runs an aggregation pipeline that performs the following actions: |
| 101 | + |
| 102 | +- Sorts the results in ascending order of their ``author`` field values |
| 103 | +- Skips the first document |
| 104 | +- Returns the remaining documents |
| 105 | + |
| 106 | +.. io-code-block:: |
| 107 | + :copyable: true |
| 108 | + |
| 109 | + .. input:: /includes/fundamentals/code-snippets/crud/skip.rs |
| 110 | + :start-after: start-aggregation-example |
| 111 | + :end-before: end-aggregation-example |
| 112 | + :language: rust |
| 113 | + :dedent: |
| 114 | + |
| 115 | + .. output:: |
| 116 | + :language: console |
| 117 | + :visible: false |
| 118 | + |
| 119 | + Document({"_id": ObjectId("..."), "name": String("Les Misérables"), "author": String("Hugo"), "length": Int32(1462)}) |
| 120 | + Document({"_id": ObjectId("..."), "name": String("A Dance with Dragons"), "author": String("Martin"), "length": Int32(1104)}) |
| 121 | + Document({"_id": ObjectId("..."), "name": String("Atlas Shrugged"), "author": String("Rand"), "length": Int32(1088)}) |
| 122 | + |
| 123 | +Additional Information |
| 124 | +---------------------- |
| 125 | + |
| 126 | +To learn more about the operations mentioned in this guide, see the following guides: |
| 127 | + |
| 128 | +- :ref:`rust-query-guide` |
| 129 | +- :ref:`rust-retrieve-guide` |
| 130 | +- :ref:`rust-compound-operations` |
| 131 | +- :ref:`rust-aggregation` |
| 132 | +- :ref:`rust-sort-guide` |
| 133 | + |
| 134 | +.. - :ref:`rust-limit-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 | +- `FindOneOptions <{+api+}/options/struct.FindOneOptions.html>`__ |
| 145 | +- `Cursor <{+api+}/struct.Cursor.html>`__ |
| 146 | +- `aggregate() <{+api+}/struct.Collection.html#method.aggregate>`__ |
| 147 | +- `AggregateOptions <{+api+}/options/struct.AggregateOptions.html>`__ |
0 commit comments