Closed marbrink closed 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
:
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.
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.
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.
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.
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?
Yes, it is. Please see my comment above that hopefully explains why that's the case.
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.
@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.
@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
Thanks for letting us know. Good to hear you've got things working.
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.
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...
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);
}
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.
I think below config works. not auto-index-creation=true
spring:
data:
mongodb:
autoIndexCreation: true
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 :-)
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:
the index is created. So this method works, but the application.properties entry doesn't work.
Using Spring Boot 2.5.5