0

我有一个带有 REST 控制器的 SpringBoot 应用程序,我需要按实体属性(包括地图值)进行排序。

这是我的实体类:

@Document
data class Event(
@Id
val id: CompoundId,

@Indexed
val timestamp: Instant,

val description: String,

val values: Map<String, Any> = HashMap()
)

我的 REST 控制器 GET:

@GetMapping("/")
fun getEvents(
        @PageableDefault(sort = ["timestamp"], direction = Sort.Direction.ASC)
        pageable: Pageable?
): Collection<Event> = mongoRepository.find(pageable)

另外,我使用 MongoDB:

override fun find(pageable: Pageable?): Collection<Event> {
    Query().apply {
        pageable?.let { with(pageable) }
        return mongoTemplate.find(this, Event::class.java)
    }
}

我正在尝试提出这样的请求:http://localhost:8080?sort=values,DESC我看到顺序已更改,但我无法理解它对哪些参数进行了排序。

是否可以按地图参数排序Pageable,例如http://localhost:8080?sort=values.someKeyInTheMap,DESC

4

1 回答 1

1

当您使用http://localhost:8080?sort=values,DESC 进行排序时,Mongo 将按照以下顺序比较 BSON:

  1. MinKey(内部类型)
  2. 无效的
  3. 数字(整数、长整数、双精度数)
  4. 符号,字符串
  5. 目的
  6. 大批
  7. 二进制数据
  8. 对象标识
  9. 布尔值
  10. 日期
  11. 时间戳
  12. 正则表达式
  13. MaxKey(内部类型)

关于使用嵌套属性,不知道你用的是什么版本,但是对嵌套属性排序有问题:https ://jira.spring.io/browse/DATAREST-976?jql=text%20~% 20%22sort%20nested%22%20ORDER%20BY%20created%20DESC

于 2019-05-17T15:41:41.153 回答