对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

排序构建器

在本指南中,您可以学习;了解如何使用Java Reactive Streams驾驶员中的构建者为查询指定排序条件。

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

  • 最小数字到最大数字

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

  • 按名字字母顺序排列

构建器是Java Reactive Streams驾驶员提供的类,可帮助您构建BSON对象。要学习;了解更多信息,请参阅构建者指南。

提示

为简洁起见,您可以选择静态导入 Sorts 类的所有方法:

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

以下示例假定此静态导入。

本页上的示例使用包含以下文档的样本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 对象,您可以将该对象传递给sort() FindPublisher实例的 方法或Aggregates.sort() 。要学习;了解有关Aggregates 类的更多信息,请参阅 聚合构建器指南。

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

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

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

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" }
...

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

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

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" }
...

要组合排序条件,请使用Sorts.orderBy()静态工厂方法。 此方法可构造一个对象,其中包含排序条件的有序列表。 执行排序时,如果最左边的排序条件的结果为并列,则排序将使用列表中的下一个排序条件来确定顺序。

以下示例在date字段上按降序对样本集合中的文档进行排序,如果出现平局,则在orderTotal字段上升序排序:

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" }

您可以按文本分数对文本查询结果进行排序,该值表示搜索结果与搜索字符串的匹配程度。要指定按文本查询的文本分数排序,请使用 Sorts.metaTextScore() 静态工厂方法。

有关更多信息,请参阅 Sorts 类 API文档。