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

Comment From: wilkinsona

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.

Comment From: marbrink

I only created the AbstractMongoClientConfiguration to try the override. I didn't have one initially. I'll later try if I can provide a sample. Thank you for your quick answer.

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

Comment From: jthomae1

Hi, I don't want to open a new issue, but I encountered the same problem and tested it with Spring Boot 2.5.3 and 2.5.5 like the opener.

If I overwrite autoIndexCreation in Java the indexes are created.

I always have a AbstractMongoClientConfiguration sub-class in my project. Is it necessary to override the method if I am using the sub-class?

Comment From: wilkinsona

Yes, it is. Please see my comment above that hopefully explains why that's the case.

Comment From: NicoHeller

The behavior is still the same in Spring Boot 2.6.0. spring.data.mongodb.auto-index-creation=true' has no effect. I am not using AbstractMongoClientConfiguration right now and nothing else is configured.

Comment From: wilkinsona

@NicoHeller No changes have been made so the same behaviour is to be expected. Thus far, as far as we know, everyone who has had this problem has defined their own AbstractMongoClientConfiguration which causes the auto-configuration to back off. If you are having the problem without anything defining a AbstractMongoClientConfiguration then please provide a minimal sample that reproduces the problem and we can take a look.

Comment From: NicoHeller

@wilkinsona

Sorry. I have simply forgotten the @Document annotation above the "Entity", if that is there everything works out of the box with spring.data.mongodb.auto-index-creation=true

Comment From: wilkinsona

Thanks for letting us know. Good to hear you've got things working.

Comment From: NicoHeller

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.

Comment From: marbrink

Just to add back to this as the OP. I didn't figure out what caused the issue for me, but I couldn't recreate it in a demo project and the problem just disappeared after some further testing in my application...

Comment From: mgfzemor

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);
    }

Comment From: karnsweta92

In one of my project I am also facing similar issue and AbstractMongoClientConfiguration class does not contain the method autoIndexCreation. Kindly suggest me way. I have made true in application.properties and not using in "AbstractMongoClientConfiguration" in any of sub class.

Comment From: nikhilznr

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

spring:
  data:
    mongodb:
      autoIndexCreation: true

Comment From: asgs

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

spring: data: mongodb: autoIndexCreation: true

I can confirm. this works! Configuration like how it should be done :-)