spring-boot: 'spring.data.mongodb.auto-index-creation=true' not working

I’ve added as per the documentation the following line to my application.properties: spring.data.mongodb.auto-index-creation=true

However there is still no index created in my model-class that is annotated with @Document and the field with @Indexed.

If I overwrite autoIndexCreation in Java via:

@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {

	@Override
    protected boolean autoIndexCreation() {               
        return true;
    }
}

the index is created. So this method works, but the application.properties entry doesn’t work.

Using Spring Boot 2.5.5

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (4 by maintainers)

Most upvoted comments

More precisely, also for other people, a POJO without @Document/@Entity annotation gets persisted into the MongoDb and the Collection is also created. Only the annotation with @Document causes the index creation.

Hi, just to contribute I was with my configs all set, but it was not working, then a did the following change in my MongoConfig :

Before (not creating indexes)

  @Bean(name = "QualifierName")
  public MongoTemplate mongoTemplate() {
      ConnectionString connectionString = new ConnectionString(this.uri);
      MongoDatabaseFactory clientDatabaseFactory = new SimpleMongoClientDatabaseFactory(connectionString);
      return new MongoTemplate(clientDatabaseFactory);
  }

After (creating indexes) - just added MappingMongoConverter

    @Bean(name = "QualifierName")
    public MongoTemplate mongoTemplate(MappingMongoConverter converter) {
        ConnectionString connectionString = new ConnectionString(this.uri);
        MongoDatabaseFactory clientDatabaseFactory = new SimpleMongoClientDatabaseFactory(connectionString);
        return new MongoTemplate(clientDatabaseFactory, converter);
    }