For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Sorts Builders

In this guide, you can learn how to specify sort criteria for your queries using builders in the Java Reactive Streams driver.

Sort criteria are the rules MongoDB uses to sort your data. Some examples of sort criteria are:

  • Smallest number to largest number

  • Earliest time of day to latest time of day

  • Alphabetical order by first name

Builders are classes provided by the Java Reactive Streams driver that help you construct BSON objects. To learn more, see the builders guide.

Tip

For brevity, you may choose to import all methods of the Sorts class statically:

import static com.mongodb.client.model.Sorts.*;

The following examples assume this static import.

The examples on this page use a sample collection that contains the following documents:

{ "_id": 1, "date": "2022-01-03", "orderTotal": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" },
{ "_id": 2, "date": "2022-01-11", "orderTotal": 83.87, "description": "two medium strawberry birthday cakes" },
{ "_id": 3, "date": "2022-01-11", "orderTotal": 19.49, "description": "1 dozen strawberry cupcakes" },
{ "_id": 4, "date": "2022-01-15", "orderTotal": 43.62, "description": "2 chicken lunches and a diet coke" },
{ "_id": 5, "date": "2022-01-23", "orderTotal": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" },
{ "_id": 6, "date": "2022-01-23", "orderTotal": 60.31, "description": "one large strawberry and chocolate cake" }

The Sorts class is a builder that provides static factory methods for all sort criteria operators supported by MongoDB. These methods return a Bson object that you can pass to the sort() method of a FindPublisher instance or to Aggregates.sort(). To learn more about the Aggregates class, see the Aggregates builder guide.

For more information about the classes and interfaces in this section, see the following API Documentation:

To specify an ascending sort, use the Sorts.ascending() static factory method. Pass Sorts.ascending() the name of the field to sort on.

The following example sorts the documents in the sample collection by ascending order on the _id field:

FindPublisher<Document> findPublisher = collection.find().sort(ascending("_id"));
Flux.from(findPublisher)
.doOnNext(doc -> System.out.println(doc.toJson()))
.blockLast();
{ "_id": 1, "date": "2022-01-03", "orderTotal": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" }
{ "_id": 2, "date": "2022-01-11", "orderTotal": 83.87, "description": "two medium strawberry birthday cakes" }
{ "_id": 3, "date": "2022-01-11", "orderTotal": 19.49, "description": "1 dozen strawberry cupcakes" }
...

To specify a descending sort, use the Sorts.descending() static factory method. Pass Sorts.descending() the name of the field to sort on.

The following example sorts the documents in the sample collection in descending order on the _id field:

FindPublisher<Document> findPublisher = collection.find().sort(descending("_id"));
Flux.from(findPublisher)
.doOnNext(doc -> System.out.println(doc.toJson()))
.blockLast();
{ "_id": 6, "date": "2022-01-23", "orderTotal": 60.31, "description": "one large strawberry and chocolate cake" }
{ "_id": 5, "date": "2022-01-23", "orderTotal": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" }
{ "_id": 4, "date": "2022-01-15", "orderTotal": 43.62, "description": "2 chicken lunches and a diet coke" }
...

To combine sort criteria, use the Sorts.orderBy() static factory method. This method constructs an object containing an ordered list of sort criteria. When performing the sort, if the leftmost sort criteria results in a tie, the sort uses the next sort criteria in the list to determine the order.

The following example sorts the documents in the sample collection in descending order on the date field, and in the event of a tie, ascending order on the orderTotal field:

Bson orderBySort = orderBy(descending("date"), ascending("orderTotal"));
FindPublisher<Document> findPublisher = collection.find().sort(orderBySort);
Flux.from(findPublisher)
.doOnNext(doc -> System.out.println(doc.toJson()))
.blockLast();
{ "_id": 5, "date": "2022-01-23", "orderTotal": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" }
{ "_id": 6, "date": "2022-01-23", "orderTotal": 60.31, "description": "one large strawberry and chocolate cake" }
{ "_id": 4, "date": "2022-01-15", "orderTotal": 43.62, "description": "2 chicken lunches and a diet coke" }
{ "_id": 3, "date": "2022-01-11", "orderTotal": 19.49, "description": "1 dozen strawberry cupcakes" }
{ "_id": 2, "date": "2022-01-11", "orderTotal": 83.87, "description": "two medium strawberry birthday cakes" }
{ "_id": 1, "date": "2022-01-03", "orderTotal": 17.86, "description": "1/2 lb cream cheese and 1 dozen bagels" }

You can sort text query results by their text score, a value that indicates how closely a search result matches your search string. To specify a sort by the text score of a text query, use the Sorts.metaTextScore() static factory method.

For more information, see the Sorts class API Documentation.