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;
}
}
See Question&Answers more detail:os