spring.data.mongodb.auto-index-creation = true
@Configuration
public class Config extends AbstractMongoClientConfiguration {
@Override
protected boolean autoIndexCreation() {
return true;
}
// ...
}
MongoDatabaseFactory dbFactory = new SimpleMongoClientDatabaseFactory(...);
DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(dbFactory);
MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.setAutoIndexCreation(true);
// ...
mappingContext.afterPropertiesSet();
MongoTemplate template = new MongoTemplate(dbFactory, new MappingMongoConverter(dbRefResolver, mappingContext));