I went through https://docs.spring.io/spring-data/mongodb/docs/2.2.0.RC1/reference/html/#mapping-usage and other sources on the web, but the solution did not worked for me.
I am using Spring Boot 2.2.2.RELEASE
and Spring Data Mongo
. In this example, at Model/Pojo field level We're using
@Indexed(name = AppConstants.FIRSTNAME_INDEX, direction = IndexDirection.ASCENDING)
private String firstName;
Error:
Please use 'MongoMappingContext#setAutoIndexCreation(boolean)' or override 'MongoConfigurationSupport#autoIndexCreation()' to be explicit.
However, we recommend setting up indices manually in an application ready block. You may use index derivation there as well.
> -----------------------------------------------------------------------------------------
> @EventListener(ApplicationReadyEvent.class)
> public void initIndicesAfterStartup() {
>
> IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
>
> IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
> resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
> }
> -----------------------------------------------------------------------------------------
As suggested in the log, I implemented, but I dont see method setAutoIndexCreation.
public class MongoConfig extends AbstractMongoClientConfiguration {
@Override
public MongoClient mongoClient() {
// TODO Auto-generated method stub
return null;
}
@Override
protected String getDatabaseName() {
// TODO Auto-generated method stub
return null;
}
}
But the solution doensn't work well, at least within the reactive context. The
mongoMappingContext.getPersistentEntities()
is empty at this time. Onlyauto-index-creation: true
(or evenmongoMappingContext.setAutoIndexCreation(true)
) ensure the creation of indexes.tried with the following configuration, I even created an own mappingContext bean
still the warning comes.I event tried spring.data.mongodb.auto-index-creation=false in application.properties, also no luck
Disable auto index creation in application.properties file
or application.yml file
Create the class MongoConfiguration whit @Configuration annotation
Injetc this dependencies
and add this method
remember that var is only for java 11+
The final class, which uses Lombok