17

I am working on an application using Spring Data MongoDB. I would like to create a compound index on one of my models. I have added a @CompoundIndex annotation at the top like so:

@Document
@CompoundIndexes({
    @CompoundIndex(name = "name_", def = "{ 'tenantId': 1, 'name': 1 }", unique = true)
})
public class MyModel {

}

However, the index is not created. I have also tried to directly place the @CompoundIndex above the class. The collection is still missing the index. The same index definition is working fine when created like so:

mongoTemplate.indexOps(MyModel.class).ensureIndex(new Index().named("name_").on("tenantId", Direction.ASC).on("name", Direction.ASC).unique());

I'd prefer to use the annotation-based definition of the index. Any ideas why this is not working?

7
  • Have you tried pastebin.com/PynPCgRY ? Commented Jun 11, 2015 at 7:42
  • I did now and it does not change the behavior... Commented Jun 11, 2015 at 8:03
  • Hi! May I ask you to post the whole application ? It would be great if you're able to provide the Spring context at least. Commented Nov 17, 2015 at 22:35
  • I had the same problem, index was not created using the annotation, but it was created using ensureIndex explicitly. The i realised i was missing the Document annotaton. Now the index is created with the CompundIndex annotation. Commented Jun 21, 2016 at 10:38
  • I'm having the same problem right now in Spring Boot 1.4 with Spring Data Mongo 1.9.2 and the Mongo 3.2 java driver. Commented Sep 8, 2016 at 19:28

3 Answers 3

18

You need to add the following to application.properties:

spring.data.mongodb.auto-index-creation=true

or in application.yml:

spring:
  data:
    mongodb:
      auto-index-creation: true
Sign up to request clarification or add additional context in comments.

1 Comment

4 years later and finding this bit of documentation is still difficult. but it fixed my problem - thank you!
5

Couple years later, I faced the same issue. If none of above answers work for you (just like for me), try overriding autoIndexCreation() in MongoConfig class.

@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {
  @Override
  protected boolean autoIndexCreation() {
    return true;
  }
}

1 Comment

I tried both and each solution separately from @Ja'afar Naddaf and @Wiktor. But only Wiktor's solution works for me, It doesn't require spring.data.mongodb.auto-index-creation=true. I'm using spring-boot-starter-data-mongodb:3.1.9
0

In this case I would rather go with mongodb createIndex method as it ensures indexes have been created, even if you created them in your app models (Spring boot in this case) it's always better to do a double check and create them manually https://docs.mongodb.com/v3.2/reference/method/db.collection.createIndex/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.