Docs 菜单
Docs 主页
/ / /
Kotlin 协程
/ /

排序构建器

在此页面上

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

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

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

  • 最小数字到最大数字

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

  • 按名字字母顺序排列

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

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

要了解 Kotlin 驱动程序中排序的基础知识,请参阅排序指南。

本页上的示例使用包含以下文档的样本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 vanilla birthday cakes" },
{ "_id": 3, "date": "2022-01-11", "orderTotal": 19.49, "description": "1 dozen vanilla 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": 60.31, "description": "one large vanilla and chocolate cake" },
{ "_id": 6, "date": "2022-01-23", "orderTotal": 10.99, "description": "1 bagel, 1 orange juice, 1 muffin" }

此数据使用以下 Kotlin 数据类进行建模:

data class Order(
@BsonId val id: Int,
val date: String,
val orderTotal: Double,
val description: String,
)

提示

构建器方法和数据类属性

通过将可选的Kotlin驾驶员扩展依赖项添加到应用程序,您可以直接将构建器类中的方法与数据类属性一起使用。 要学习;了解更多信息和查看示例,请参阅《使用构建器与数据类》指南。

Sorts类是一个构建器,可为 MongoDB 支持的所有排序条件操作符提供静态工厂方法。 这些方法会返回一个Bson对象,您可以将其传递给FindFlow实例的sort()方法或Aggregates.sort()

要了解有关Aggregates类的详情,请参阅聚合构建器指南。

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

  • 排序

  • BSON

  • FindFlow

  • 聚合

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

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

val resultsFlow = collection.find()
.sort(Sorts.ascending(Order::orderTotal.name))
resultsFlow.collect { println(it) }
Order(id=6, date=2022-01-23, orderTotal=10.99, description=1 bagel, 1 orange juice, 1 muffin)
Order(id=1, date=2022-01-03, orderTotal=17.86, description=1/2 lb cream cheese and 1 dozen bagels)
Order(id=3, date=2022-01-11, orderTotal=19.49, description=1 dozen vanilla cupcakes)
Order(id=4, date=2022-01-15, orderTotal=43.62, description=2 chicken lunches and a diet coke)
Order(id=5, date=2022-01-23, orderTotal=60.31, description=one large vanilla and chocolate cake)
Order(id=2, date=2022-01-11, orderTotal=83.87, description=two medium vanilla birthday cakes)

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

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

val resultsFlow = collection.find()
.sort(Sorts.descending(Order::orderTotal.name))
resultsFlow.collect { println(it) }
Order(id=2, date=2022-01-11, orderTotal=83.87, description=two medium vanilla birthday cakes)
Order(id=5, date=2022-01-23, orderTotal=60.31, description=one large vanilla and chocolate cake)
Order(id=4, date=2022-01-15, orderTotal=43.62, description=2 chicken lunches and a diet coke)
Order(id=3, date=2022-01-11, orderTotal=19.49, description=1 dozen vanilla cupcakes)
Order(id=1, date=2022-01-03, orderTotal=17.86, description=1/2 lb cream cheese and 1 dozen bagels)
Order(id=6, date=2022-01-23, orderTotal=10.99, description=1 bagel, 1 orange juice, 1 muffin)

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

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

val orderBySort = Sorts.orderBy(
Sorts.descending(Order::date.name), Sorts.ascending(Order::orderTotal.name)
)
val results = collection.find().sort(orderBySort)
results.collect {println(it) }
Order(id=6, date=2022-01-23, orderTotal=10.99, description=1 bagel, 1 orange juice, 1 muffin)
Order(id=5, date=2022-01-23, orderTotal=60.31, description=one large vanilla and chocolate cake)
Order(id=4, date=2022-01-15, orderTotal=43.62, description=2 chicken lunches and a diet coke)
Order(id=3, date=2022-01-11, orderTotal=19.49, description=1 dozen vanilla cupcakes)
Order(id=2, date=2022-01-11, orderTotal=83.87, description=two medium vanilla birthday cakes)
Order(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 文档。

后退

投射