I am working on an application using Spring Data MongoDB. I would like to create a composite index on one of my models. I added the @CompoundIndex annotation at the top like this:
@Document @CompoundIndexes({ @CompoundIndex(name = "name_", def = "{ 'tenantId': 1, 'name': 1 }", unique = true) }) public class MyModel { }
However, an index is not created. I also tried directly placing @CompoundIndex over the class. The collection is still missing an index. The same index definition works fine when created like this:
mongoTemplate.indexOps(MyModel.class).ensureIndex(new Index().named("name_").on("tenantId", Direction.ASC).on("name", Direction.ASC).unique());
I would rather use an index definition based on annotations. Any ideas why this is not working?
java spring spring-data-mongodb indexing mongodb
fynn
source share