文档菜单
文档首页
/ / /
Kotlin 协程
/ /

排序构建器

本页内容

  • 概述
  • Sorts 类
  • 升序
  • 降序
  • 组合排序条件
  • 文本分数

在本指南中,您将学习如何使用MongoDB Kotlin驱动程序的builders指定查询的排序标准

排序标准是MongoDB用来排序您的数据的规则。一些排序标准的示例包括

  • 从最小的数字到大数字

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

  • 按名字的首字母顺序

Builders是Kotlin驱动程序提供的类,可以帮助您构建BSON对象。要了解更多信息,请参阅builders指南。

如果您想学习如何使用builders指定查询的排序标准,请阅读本指南。

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

本页面上的示例使用以下文档的样本集合

{ "_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,
)

TheSorts类是一个builder,它为MongoDB支持的所有的排序标准操作符提供了静态工厂方法。这些方法返回一个Bson对象,您可以将其传递给FindFlow实例的sort()方法或传递给Aggregates.sort()

要了解更多关于Aggregates类的信息,请参阅Aggregates builder指南。

关于本节中类和接口的更多信息,请参阅以下API文档

  • Sorts

  • BSON

  • FindFlow

  • Aggregates

要指定升序排序,请使用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 文档。

返回

投影