Skip to content

Commit 0aaa6fe

Browse files
authored
DOCSP-38760: sorts builders (#29)
* DOCSP-38760: sorts builders * CC change
1 parent 84b6f3f commit 0aaa6fe

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

source/builders.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ Builders
88

99
/builders/filters/
1010
/builders/projections/
11+
/builders/sorts/
1112

1213
The {+driver-short+} provides the following classes that make it easier to use
1314
the CRUD API:
1415

1516
- :ref:`scala-builders-filters`: Support for building query filters
1617
- :ref:`scala-builders-projections`: Support for building projections
18+
- :ref:`scala-builders-sorts`: Support for building sort criteria
1719

1820
.. TODO replace with links
1921

20-
- Sorts: Support for building sort criteria
2122
- Aggregation: Support for building aggregation pipelines
2223
- Updates: Support for building updates
2324
- Indexes: Support for creating index keys

source/builders/sorts.txt

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)