简体   繁体   中英

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?

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

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM