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 5 years ago
  • Comments: 16 (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);
    }

I think below config works. not auto-index-creation=true

spring:
  data:
    mongodb:
      autoIndexCreation: true

Did you always have an AbstractMongoClientConfiguration sub-class in your application, or was that only added to allow you to override autoIndexCreation()? If it was always there, the MongoMappingContext defined by its super-class, MongoConfigurationSupport, will have caused the auto-configured MongoMappingContext to back off. As a result of this backing off, spring.data.mongodb.auto-index-creation will have no effect as it’s applied to the auto-configured MongoMappingContext:

https://github.com/spring-projects/spring-boot/blob/34677508a76d0decad4ba2ced46ccfcf6804fb4a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.java#L50

If the problem occurs without an AbstractMongoClientConfiguration or MongoConfigurationSupport sub-class in your application then I’m not sure what the cause could be. If this is the case and you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.