doubleW
doubleW

Reputation: 688

CompoundIndex spring case insensitive

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?

Upvotes: 4

Views: 1434

Answers (2)

AvivZano
AvivZano

Reputation: 11

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)

Upvotes: 1

Jakub Prądzyński
Jakub Prądzyński

Reputation: 63

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()))
        )
    }

Upvotes: 3

Related Questions