排序构建器
概述
在本指南中,您将学习如何使用MongoDB Java驱动器中的构建器为您的查询指定 排序标准。
排序标准是MongoDB用来排序您的数据的规则。一些排序标准的例子包括
从小到大排序的数字
从一天最早的时间到最晚的时间
按姓氏字母顺序排序
MongoDB Java驱动程序提供的类,用于帮助您构建BSON对象。要了解更多信息,请参阅我们的构建器指南。
如果您想使用构建器来指定查询的排序标准,请阅读此指南。
如果您想了解MongoDB Java驱动程序中排序的基础知识,请考虑阅读我们的排序指南。
本页上的示例使用包含以下文档的样本集合
{ "_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" }
Sorts类
TheSorts
类是一个构建器,它提供了MongoDB支持的所有排序标准运算符的静态工厂方法。这些方法返回一个Bson
对象,您可以将其传递给FindIterable
实例的sort()
方法或传递给Aggregates.sort()
。如果您想了解更多关于Aggregates
类的信息,请参阅我们的Aggregates构建器指南。
有关本节中类和接口的更多信息,请参阅以下API文档
升序
要指定升序排序,请使用Sorts.ascending()
静态工厂方法。传递Sorts.ascending()
要排序的字段名称。
以下示例按升序顺序对样本集合中的文档进行排序
import static com.mongodb.client.model.Sorts.ascending; // <MongoCollection setup code here> collection.find().sort(ascending("_id"));
上述示例的输出类似于以下内容
{ "_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" } ...
降序
要指定降序排序,请使用 Sorts.descending()
静态工厂方法。传递 Sorts.descending()
要排序的字段名称。
以下示例按照 _id
字段以降序对 示例集合 中的文档进行排序
import static com.mongodb.client.model.Sorts.descending; // <MongoCollection setup code here> collection.find().sort(descending("_id"));
前面的示例应该输出类似以下内容
{ "_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" } ...
组合排序条件
要组合排序条件,请使用 Sorts.orderBy()
静态工厂方法。此方法构建一个包含排序条件有序列表的对象。在执行排序时,如果最左侧的排序条件结果为平局,则排序将使用列表中的下一个排序条件来决定顺序。
以下示例按照 letter
字段以降序对 示例集合 中的文档进行排序,并在平局的情况下按照 _id
字段以升序排序
import static com.mongodb.client.model.Sorts.orderBy; import static com.mongodb.client.model.Sorts.ascending; import static com.mongodb.client.model.Sorts.descending; // <MongoCollection setup code here> Bson orderBySort = orderBy(descending("date"), ascending("orderTotal")); collection.find().sort(orderBySort);
上述示例的输出类似于以下内容
{ "_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" }
文本分数
您可以通过文本分数对文本搜索结果进行排序,文本分数表示搜索结果与您的搜索字符串的匹配程度。要指定按文本搜索的文本分数排序,请使用 Sorts.metaTextScore()
静态工厂方法。有关如何使用 Sorts.metaTextScore()
方法指定排序条件的详细示例,请参阅我们排序基础指南中的文本搜索部分。
有关更多信息,请参阅Sorts 类 API 文档。