Docs 菜单
Docs 主页
/ / /
Java (Sync) 驱动程序
/ /

排序构建器

在此页面上

  • Overview
  • Sorts 类
  • 升序
  • 降序
  • 组合排序标准
  • 文本分数

在本指南中,您可以了解如何使用 MongoDB Java 驱动程序中的构建器为查询指定排序条件

排序条件是 MongoDB 用于对数据进行排序的规则。 排序条件的一些示例如下:

  • 最小数字到最大数字

  • 一天中的最早时间到一天中的最晚时间

  • 按名字字母顺序排列

构建器是 MongoDB Java 驱动程序提供的类,可帮助您构建 BSON对象。要了解更多信息,请参阅我们的构建器指南。

如果您想使用构建器来指定查询的排序条件,则应阅读本指南。

如果您想了解 MongoDB Java 驱动程序中的排序基础知识,请考虑阅读我们的排序指南。

本页上的示例使用包含以下文档的样本collection:

{ "_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类是一个构建器,可为 MongoDB 支持的所有排序条件操作符提供静态工厂方法。 这些方法会返回一个Bson对象,您可以将其传递给FindIterable实例的sort()方法或Aggregates.sort() 。 如果您想了解有关Aggregates类的更多信息,请参阅我们的聚合构建器指南。

有关此部分中的类和接口的更多信息,请参阅以下 API 文档:

  • 排序

  • BSON

  • FindIterable

  • 聚合

要指定升序排序,请使用Sorts.ascending()静态工厂方法。 向Sorts.ascending()传递要排序的字段的名称。

以下示例在_id字段上按升序对样本集合中的文档进行排序:

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 文档。

后退

投影构建器