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)
More precisely, also for other people, a POJO without
@Document/@Entityannotation gets persisted into the MongoDb and the Collection is also created. Only the annotation with@Documentcauses 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)
After (creating indexes) - just added
MappingMongoConverterI think below config works. not auto-index-creation=true
Did you always have an
AbstractMongoClientConfigurationsub-class in your application, or was that only added to allow you to overrideautoIndexCreation()? If it was always there, theMongoMappingContextdefined by its super-class,MongoConfigurationSupport, will have caused the auto-configuredMongoMappingContextto back off. As a result of this backing off,spring.data.mongodb.auto-index-creationwill have no effect as it’s applied to the auto-configuredMongoMappingContext: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
AbstractMongoClientConfigurationorMongoConfigurationSupportsub-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.