Search code examples
mongodbspring-bootkotlinspring-data

Using @Collations with Spring Data Mongo seems not to work


I'm using Spring data mongo and want to use collations for various queries. The collations need to work with the default repository methods as well as with custom queries.

According to the reference doc and the initial feature description,

the following should work:

@Collation(value = "...")
data class SampleDocument(
    @field:MongoId
    var id: String? = null,
    val value: String
)

interface RepositoryWithCollationInQuery: CrudRepository<SampleDocument, String> {
    @Collation(value = "...")
    @Query(
        value = "{}",
        sort = """{ "value":  1}""",
    )
    fun findAllCollationInAnnotation(): List<SampleDocument>
}

But the collation is not evaluated. It is only evaluated when adding it to @Query directly.

Versions:

  • Spring Boot: 3.1.5
  • Spring Data Mongo: 4.1.5
  • MongoDB: 6 (4 showed no differences)

A sample project with the expected code (and failing tests) can be found here: https://github.com/dirkbolte/mongodb_collation_test

The following approaches were tried:

  • adding @Collation to the data class (failed)
  • adding @Collation to the repository interface (failed)
  • adding @Collation to the query method (failed)
  • specifying collation=... on the @Query annotation (worked)

I would expect that adding the @Collation annotation to repository and/or document would allow all repository methods to make use of it, especially when using findAll(pageable) . When requiring @Query for everything, I would have to specify every query manually and explicitly.


Solution

  • Answering myself: issue is accepted as bug in Spring Data Mongo