4

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?

5
  • You can always preview 2.0.0.RC2
    – Neil Lunn
    Aug 9, 2017 at 12:32
  • 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
    Aug 9, 2017 at 12:43
  • @doubleW I am also looking for same information. Did you find any solution ?
    – MIM
    Nov 27, 2018 at 13:35
  • @MIM unfortunatelly no and currently I am not working on this project any more
    – doubleW
    Nov 29, 2018 at 10:19
  • @doubleW Ok thanks, I am proceeding with strength collation as of now.
    – MIM
    Nov 30, 2018 at 7:11

2 Answers 2

3

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

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.