spring-projects / spring-projects/spring-data-mongodb

@Collation annotation on documents, repositories or queries not interpreted

Open
#4,535 4 comments 2 reactions 1 assignee View on GitHub

@christophstrobl is already working on this.

Since Oct 23, 2023.

in: repository type: bug
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

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 #2755 , 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.

A sample project with the expected usage (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) of the default spring repository interfaces. When requiring @Query for everything, I would have to specify every query manually and explicitly, which doesn't feel right.

Versions:

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

For reference, the I asked the question on stack overflow as well, in case this is a user error.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.