spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.99k stars 40.65k forks source link

'spring.data.mongodb.auto-index-creation=true' not working #28478

Closed marbrink closed 2 years ago

marbrink commented 2 years ago

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

wilkinsona commented 2 years ago

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.

marbrink commented 2 years ago

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.

spring-projects-issues commented 2 years ago

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.

spring-projects-issues commented 2 years ago

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.

jthomae1 commented 2 years ago

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?

wilkinsona commented 2 years ago

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

NicoHeller commented 2 years ago

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.

wilkinsona commented 2 years ago

@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.

NicoHeller commented 2 years ago

@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

wilkinsona commented 2 years ago

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

NicoHeller commented 2 years ago

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.

marbrink commented 2 years ago

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...

mgfzemor commented 1 year ago

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);
    }
karnsweta92 commented 1 year ago

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.

nikhilznr commented 11 months ago

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

spring:
  data:
    mongodb:
      autoIndexCreation: true
asgs commented 5 months ago

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 :-)