@CompoundIndex not working in Spring Data MongoDB

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?

+7
java spring spring-data-mongodb indexing mongodb
source share
1 answer

In this case, I prefer to use the mongodb createIndex method, since it provides the creation of indexes, even if you created them in your application models (Spring boot in this case), it is always better to double check and create them manually https: //docs.mongodb .com / v3.2 / reference / method / db.collection.createIndex /

0
source share

All Articles