排序构建器
Overview
在本指南中,您可以了解如何使用 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( val id: Int, val date: String, val orderTotal: Double, val description: String, )
Sorts 类
Sorts
类是一个构建器,可为 MongoDB 支持的所有排序条件操作符提供静态工厂方法。 这些方法会返回一个Bson
对象,您可以将其传递给FindFlow
实例的sort()
方法或Aggregates.sort()
。
要了解有关Aggregates
类的详情,请参阅聚合构建器指南。
有关此部分中的类和接口的更多信息,请参阅以下 API 文档:
升序
要指定升序排序,请使用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 文档。