|
| 1 | +.. _rust-sort-guide: |
| 2 | + |
| 3 | +============ |
| 4 | +Sort Results |
| 5 | +============ |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, read operation, sort, sort 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 **sort** |
| 24 | +operations to specify the order of your read operation results. |
| 25 | + |
| 26 | +Use the ``sort()`` method when building options to change the order in which |
| 27 | +read operations return documents. The ``sort()`` method tells MongoDB to order |
| 28 | +returned documents by the values of one or more fields in a certain direction. |
| 29 | +To sort returned documents by a field in ascending (lowest first) order, use a |
| 30 | +value of ``1``. To sort in descending (greatest first) order instead, use |
| 31 | +``-1``. If you do not specify a sort, MongoDB does not guarantee the order of |
| 32 | +query results. |
| 33 | + |
| 34 | +Sample Data for Examples |
| 35 | +------------------------ |
| 36 | + |
| 37 | +The examples in this guide use the following ``Book`` struct as a model for |
| 38 | +documents in the ``books`` collection: |
| 39 | + |
| 40 | +.. literalinclude:: /includes/fundamentals/code-snippets/crud/sort.rs |
| 41 | + :start-after: start-book-struct |
| 42 | + :end-before: end-book-struct |
| 43 | + :language: rust |
| 44 | + :dedent: |
| 45 | + |
| 46 | +The following code shows how to insert sample data into the ``books`` |
| 47 | +collection: |
| 48 | + |
| 49 | +.. literalinclude:: /includes/fundamentals/code-snippets/crud/sort.rs |
| 50 | + :start-after: start-sample-data |
| 51 | + :end-before: end-sample-data |
| 52 | + :language: rust |
| 53 | + :dedent: |
| 54 | + |
| 55 | +Methods for Sorting |
| 56 | +------------------- |
| 57 | + |
| 58 | +You can sort results retrieved by a query, or you can sort results within an |
| 59 | +aggregation pipeline. |
| 60 | + |
| 61 | +Chain the ``sort()`` method to the ``find()`` method to sort results retrieved |
| 62 | +by the query, as shown in the following example: |
| 63 | + |
| 64 | +.. literalinclude:: /includes/fundamentals/code-snippets/crud/sort.rs |
| 65 | + :start-after: start-sort-query |
| 66 | + :end-before: end-sort-query |
| 67 | + :language: rust |
| 68 | + :dedent: |
| 69 | + |
| 70 | +Options |
| 71 | +~~~~~~~ |
| 72 | + |
| 73 | +Alternatively, you can use the ``sort()`` method of the ``FindOptions`` |
| 74 | +struct. |
| 75 | + |
| 76 | +The following example performs a ``find()`` operation with the following behavior: |
| 77 | + |
| 78 | +- Sorts the results in ascending order of the values of the ``author`` field |
| 79 | +- Skips the first document |
| 80 | +- Returns the remaining documents |
| 81 | + |
| 82 | +.. literalinclude:: /includes/fundamentals/code-snippets/crud/sort.rs |
| 83 | + :start-after: start-sort-query-multiple-options |
| 84 | + :end-before: end-sort-query-multiple-options |
| 85 | + :language: rust |
| 86 | + :dedent: |
| 87 | + |
| 88 | +Aggregation |
| 89 | +~~~~~~~~~~~ |
| 90 | + |
| 91 | +To sort your results within an aggregation pipeline, create a ``$sort`` stage |
| 92 | +and pass the list of stages to the ``aggregate()`` method. |
| 93 | + |
| 94 | +The following example shows how to create a ``$sort`` stage that sorts documents |
| 95 | +in ascending order by the values of the ``author`` field: |
| 96 | + |
| 97 | +.. literalinclude:: /includes/fundamentals/code-snippets/crud/sort.rs |
| 98 | + :start-after: start-sort-aggregation |
| 99 | + :end-before: end-sort-aggregation |
| 100 | + :language: rust |
| 101 | + :dedent: |
| 102 | + |
| 103 | +Sorting Direction |
| 104 | +----------------- |
| 105 | + |
| 106 | +The direction of your sort can either be **ascending** or **descending**. An |
| 107 | +ascending sort orders your results from smallest to largest. A descending sort |
| 108 | +orders your results from largest to smallest. |
| 109 | + |
| 110 | +The following list contains examples of data sorted in ascending order: |
| 111 | + |
| 112 | +* Numbers: 1, 2, 3, 43, 43, 55, 120 |
| 113 | +* Dates: 1990-03-10, 1995-01-01, 2005-10-30, 2005-12-21 |
| 114 | +* Words (ASCII): Banana, Dill, carrot, cucumber, hummus |
| 115 | + |
| 116 | +The following list contains examples of data sorted in descending order: |
| 117 | + |
| 118 | +* Numbers: 100, 30, 12, 12, 9, 3, 1 |
| 119 | +* Dates: 2020-01-01, 1998-12-11, 1998-12-10, 1975-07-22 |
| 120 | +* Words (reverse ASCII): pear, grapes, apple, Cheese |
| 121 | + |
| 122 | +The following subsections show how to specify these sort criteria. |
| 123 | + |
| 124 | +Ascending |
| 125 | +~~~~~~~~~ |
| 126 | + |
| 127 | +To specify an ascending sort, pass the field you want to sort by and ``1`` to |
| 128 | +the ``sort()`` method. |
| 129 | + |
| 130 | +Example |
| 131 | +^^^^^^^ |
| 132 | + |
| 133 | +The following example specifies an ascending sort on the ``name`` field: |
| 134 | + |
| 135 | +.. io-code-block:: |
| 136 | + :copyable: true |
| 137 | + |
| 138 | + .. input:: /includes/fundamentals/code-snippets/crud/sort.rs |
| 139 | + :start-after: start-ascending-sort |
| 140 | + :end-before: end-ascending-sort |
| 141 | + :language: rust |
| 142 | + :dedent: |
| 143 | + |
| 144 | + .. output:: |
| 145 | + :language: console |
| 146 | + :visible: false |
| 147 | + |
| 148 | + Book { "_id": 4, "name": "A Dance with Dragons", "author": Martin, "length": 1104 } |
| 149 | + Book { "_id": 2, "name": "Atlas Shrugged", "author": Rand, "length": 1088 } |
| 150 | + Book { "_id": 3, "name": "Les Miserables", "author": Hugo, "length": 1462 } |
| 151 | + Book { "_id": 1, "name": "The Brothers Karamazov", "author": Dostoevsky, "length": 824 } |
| 152 | + |
| 153 | +Descending |
| 154 | +~~~~~~~~~~ |
| 155 | + |
| 156 | +To specify a descending sort, pass the field you want to sort by and ``-1`` to |
| 157 | +the ``sort()`` method. |
| 158 | + |
| 159 | +Example |
| 160 | +^^^^^^^ |
| 161 | + |
| 162 | +The following example specifies a descending sort on the ``name`` field: |
| 163 | + |
| 164 | +.. io-code-block:: |
| 165 | + :copyable: true |
| 166 | + |
| 167 | + .. input:: /includes/fundamentals/code-snippets/crud/sort.rs |
| 168 | + :start-after: start-descending-sort |
| 169 | + :end-before: end-descending-sort |
| 170 | + :language: rust |
| 171 | + :dedent: |
| 172 | + |
| 173 | + .. output:: |
| 174 | + :language: console |
| 175 | + :visible: false |
| 176 | + |
| 177 | + Book { "_id": 1, "name": "The Brothers Karamazov", "author": Dostoevsky, "length": 824 } |
| 178 | + Book { "_id": 3, "name": "Les Miserables", "author": Hugo, "length": 1462 } |
| 179 | + Book { "_id": 2, "name": "Atlas Shrugged", "author": Rand, "length": 1088 } |
| 180 | + Book { "_id": 4, "name": "A Dance with Dragons", "author": Martin, "length": 1104 } |
| 181 | + |
| 182 | +Additional Information |
| 183 | +---------------------- |
| 184 | + |
| 185 | +To learn more about the operations metnioned in this guide, see the following: |
| 186 | + |
| 187 | +- :ref:`rust-query-guide` |
| 188 | +- :ref:`rust-retrieve-guide` |
| 189 | +- :ref:`rust-compound-operations` |
| 190 | +- :ref:`rust-aggregation` |
| 191 | + |
| 192 | +API Documentation |
| 193 | +~~~~~~~~~~~~~~~~~ |
| 194 | + |
| 195 | +To learn more about any of the methods or types discussed in this guide, see the |
| 196 | +following API Documentation: |
| 197 | + |
| 198 | +- `find() <{+api+}/struct.Collection.html#method.find>`__ |
| 199 | +- `FindOptions <{+api+}/options/struct.FindOptions.html>`__ |
| 200 | +- `FindOneOptions <{+api+}/options/struct.FindOneOptions.html>`__ |
| 201 | +- `Cursor <{+api+}/struct.Cursor.html>`__ |
| 202 | +- `aggregate() <{+api+}/struct.Collection.html#method.aggregate>`__ |
| 203 | +- `AggregateOptions <{+api+}/options/struct.AggregateOptions.html>`__ |
0 commit comments