|
| 1 | +.. _scala-builders-sorts: |
| 2 | + |
| 3 | +===== |
| 4 | +Sorts |
| 5 | +===== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, consistent results, order results |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +The ``Sorts`` class provides static factory methods for the MongoDB |
| 21 | +sort criteria operators. Each method returns an instance of the ``Bson`` |
| 22 | +type, which can in turn be passed to any method that expects sort |
| 23 | +criteria. |
| 24 | + |
| 25 | +You can import the methods of the ``Sorts`` |
| 26 | +class statically, as shown in the following code: |
| 27 | + |
| 28 | +.. code-block:: scala |
| 29 | + |
| 30 | + import org.mongodb.scala.model.Sorts._ |
| 31 | + |
| 32 | +The examples in this guide assume this static import. |
| 33 | + |
| 34 | +Ascending |
| 35 | +--------- |
| 36 | + |
| 37 | +To specify an ascending sort, use one of the ``ascending()`` methods. |
| 38 | + |
| 39 | +The following example specifies an ascending sort on the ``quantity`` |
| 40 | +field: |
| 41 | + |
| 42 | +.. code-block:: scala |
| 43 | + |
| 44 | + ascending("quantity") |
| 45 | + |
| 46 | +This example specifies an ascending sort on the ``quantity`` field, followed |
| 47 | +by an ascending sort on the ``totalAmount`` field: |
| 48 | + |
| 49 | +.. code-block:: scala |
| 50 | + |
| 51 | + ascending("quantity", "totalAmount") |
| 52 | + |
| 53 | +Descending |
| 54 | +---------- |
| 55 | + |
| 56 | +To specify a descending sort, use one of the ``descending()`` methods. |
| 57 | + |
| 58 | +This example specifies a descending sort on the ``quantity`` field: |
| 59 | + |
| 60 | +.. code-block:: scala |
| 61 | + |
| 62 | + descending("quantity") |
| 63 | + |
| 64 | +This example specifies a descending sort on the ``quantity`` field, |
| 65 | +followed by a ``descending`` sort on the ``totalAmount`` field: |
| 66 | + |
| 67 | +.. code-block:: scala |
| 68 | + |
| 69 | + descending("quantity", "totalAmount") |
| 70 | + |
| 71 | +Text Score |
| 72 | +---------- |
| 73 | + |
| 74 | +To specify a sort on the score of a ``$text`` query, use the |
| 75 | +``metaTextScore()`` method to specify the name of the projected field. |
| 76 | + |
| 77 | +This example specifies a descending sort on the score of a ``$text`` query that will be |
| 78 | +projected into the ``scoreValue`` field: |
| 79 | + |
| 80 | +.. code-block:: scala |
| 81 | + |
| 82 | + metaTextScore("scoreValue") |
| 83 | + |
| 84 | +Combining Sorts |
| 85 | +--------------- |
| 86 | + |
| 87 | +To combine multiple sort criteria, use the ``orderBy()`` method. |
| 88 | + |
| 89 | +This example specifies ascending sorts on the ``quantity`` and |
| 90 | +``totalAmount`` fields, followed by a descending sort on the |
| 91 | +``orderDate`` field: |
| 92 | + |
| 93 | +.. code-block:: scala |
| 94 | + |
| 95 | + orderBy(ascending("quantity", "totalAmount"), descending("orderDate")) |
0 commit comments