Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
marbrink opened this issue Oct 29, 2021 · 16 comments
Closed

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

marbrink opened this issue Oct 29, 2021 · 16 comments

Comments

@marbrink
Copy link

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 29, 2021
@wilkinsona
Copy link
Member

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.

@marbrink
Copy link
Author

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 spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Oct 29, 2021
@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Oct 29, 2021
@spring-projects-issues
Copy link
Collaborator

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 spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Nov 5, 2021
@spring-projects-issues
Copy link
Collaborator

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.

@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged labels Nov 12, 2021
@jthomae1
Copy link

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
Copy link
Member

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

@NicoHeller
Copy link

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
Copy link
Member

wilkinsona commented Nov 20, 2021

@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
Copy link

NicoHeller commented Nov 20, 2021

@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
Copy link
Member

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

@NicoHeller
Copy link

NicoHeller commented Nov 21, 2021

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
Copy link
Author

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
Copy link

mgfzemor commented Mar 1, 2023

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
Copy link

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

This comment was marked as outdated.

@asgs

This comment was marked as outdated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants