3
votes

So, I am working on indexes in MongoDB from Spring level. I want to use the case insensitive index.

https://docs.mongodb.com/v3.4/core/index-case-insensitive/

From above mongo documentation I can see that from DB level it can be done by using the strength collation and should be used in createIndex function. But I was unable to find any information about how to use the options in the CompoundIndex annotation.

http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/index/CompoundIndex.html

On Spring dosc there is no word about options. Anyone has a clue how to do it?

2
You can always preview 2.0.0.RC2 - Neil Lunn
I was looking there also. But did not found any information about the options possible to add. It looks like there is no support for this parameter in the CompoundIndex annotation, but I think that would be quite strange - doubleW
@doubleW I am also looking for same information. Did you find any solution ? - MIM
@MIM unfortunatelly no and currently I am not working on this project any more - doubleW
@doubleW Ok thanks, I am proceeding with strength collation as of now. - MIM

2 Answers

2
votes

I didn't find such an option in annotations like @Indexed, but you can use something like this to ensure index exists with collation:

@Configuration
@DependsOn("mongoTemplate")
class CollectionConfig(@Autowired private val mongoTemplate: MongoTemplate) {

    @PostConstruct
    fun ensureIndexes() {
        mongoTemplate.indexOps(DbObject::class.java).ensureIndex(
                Index("fieldName", Sort.Direction.ASC)
                        .unique()
                        .background()
                     .collation(of(Locale.ENGLISH).strength(ComparisonLevel.secondary()))
        )
    }
0
votes

I was looking for the same information and found out that you can use this:

@CompoundIndex(name = "name_field", def = "{field1: 1,field2:1}{collation:'en', strength:2}")

it's not mentioned in the documentation but I found an open issue about that (https://jira.spring.io/browse/DATAMONGO-2133)