文档菜单
文档首页
/ / /
Scala
/

排序方式

本页

  • 升序
  • 降序
  • 文本得分
  • 组合排序

主题Sorts 类提供用于 MongoDB 排序条件的静态工厂方法。每个方法返回一个 Bson 类型的实例,该实例可以传递给任何期望排序条件的任何方法。Bson 类型,它可以进一步传递给任何期望排序条件的任何方法。

您可以将 Sorts 类的方法静态导入,如下面的代码所示

import org.mongodb.scala.model.Sorts._

本指南中的示例假设这种静态导入。

要指定升序排序,请使用 ascending() 方法之一。

以下示例指定对 quantity 字段的升序排序

ascending("quantity")

以下示例指定对 quantity 字段的升序排序,然后是对 totalAmount 字段的升序排序

ascending("quantity", "totalAmount")

要指定降序排序,请使用 descending() 方法之一。

以下示例指定对 quantity 字段的降序排序

descending("quantity")

以下示例指定对 quantity 字段的降序排序,然后是对 totalAmount 字段的降序排序

descending("quantity", "totalAmount")

要指定对查询 $text 的得分的排序,使用 metaTextScore() 方法指定投影字段的名称。

以下示例指定了对将投影到 scoreValue 字段的 $text 查询得分的降序排序

metaTextScore("scoreValue")

要组合多个排序条件,请使用 orderBy() 方法。

以下示例指定了按 quantitytotalAmount 字段的升序排序,接着是按 orderDate 字段的降序排序

orderBy(ascending("quantity", "totalAmount"), descending("orderDate"))

返回

投影