- Builders
- Sort Criteria
Sorts
The Sorts
class provides static factory methods for all the MongoDB sort criteria
operators. Each method returns an instance of the Bson
type, which can in turn be passed to
any method that expects sort criteria.
For brevity, you may choose to import the methods of the Sorts
class statically:
import static com.mongodb.client.model.Sorts.*;
All the examples below assume this static import.
Ascending
To specify an ascending sort, use one of the ascending
methods.
This example specifies an ascending sort on the quantity
field:
ascending("quantity")
This example specifies an ascending sort on the quantity
field, followed by an ascending sort on the totalAmount
field:
ascending("quantity", "totalAmount")
Descending
To specify a descending sort, use one of the descending
methods.
This example specifies a descending sort on the quantity
field:
descending("quantity")
This example specifies a descending sort on the quantity
field, followed by a descending sort on the totalAmount
field:
descending("quantity", "totalAmount")
Text Score
To specify a sort by the score of a $text
query, use the
metaTextScore
method to specify the name of the projected field.
This example specifies a sort on the score of a $text
query that will be projected into the scoreValue
field in a projection on the
same query:
metaTextScore("scoreValue")
Combining sort criteria
To specify the combination of multiple sort criteria, use the orderBy
method.
This example specifies an ascending sort on the quantity
field, followed by an ascending sort on the totalAmount
field, followed by a
descending sort on the orderDate
field:
orderBy(ascending("quantity", "totalAmount"), descending("orderDate"))