0

I have a SpringBoot application with REST controller and I need to do sorting by entity properties including map values.

Here is my entity class:

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

@Indexed
val timestamp: Instant,

val description: String,

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

My REST controller GET:

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

Also, I use MongoDB:

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

I am trying to make requests like this: http://localhost:8080?sort=values,DESC and I see that order is changed, but I can't understand which parameters it sorted.

Is it possible to sort by map parameters with Pageable, like http://localhost:8080?sort=values.someKeyInTheMap,DESC ?

Yann39
  • 14,285
  • 11
  • 56
  • 84
Valeriy K.
  • 2,616
  • 1
  • 30
  • 53

1 Answers1

1

When you sort using http://localhost:8080?sort=values,DESC, Mongo will compare BSON, following the order:

  1. MinKey (internal type)
  2. Null
  3. Numbers (ints, longs, doubles)
  4. Symbol, String
  5. Object
  6. Array
  7. BinData
  8. ObjectId
  9. Boolean
  10. Date
  11. Timestamp
  12. Regular Expression
  13. MaxKey (internal type)

About using nested properties, I don't know what version are you using, but there is an issue about sorting on nested properties: https://jira.spring.io/browse/DATAREST-976?jql=text%20~%20%22sort%20nested%22%20ORDER%20BY%20created%20DESC

viniciusjssouza
  • 1,235
  • 14
  • 28