Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please use 'MongoMappingContext#setAutoIndexCreation(boolean)' or override 'MongoConfigurationSupport#autoIndexCreation()' to be explicit

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 work 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 don't 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;
    }
}
like image 887
PAA Avatar asked Jan 31 '20 11:01

PAA


2 Answers

Disable auto index creation in application.properties file

spring.data.mongodb.auto-index-creation=false

or application.yml file

spring:
  data:
    mongodb:
      auto-index-creation: false

Create the class MongoConfiguration whit @Configuration annotation

Injetc this dependencies

private final MongoTemplate mongoTemplate;
private final MongoConverter mongoConverter;

and add this method

@EventListener(ApplicationReadyEvent.class)
public void initIndicesAfterStartup() {

    log.info("Mongo InitIndicesAfterStartup init");
    var init = System.currentTimeMillis();

    var mappingContext = this.mongoConverter.getMappingContext();

    if (mappingContext instanceof MongoMappingContext) {
        MongoMappingContext mongoMappingContext = (MongoMappingContext) mappingContext;
        for (BasicMongoPersistentEntity<?> persistentEntity : mongoMappingContext.getPersistentEntities()) {
            var clazz = persistentEntity.getType();
            if (clazz.isAnnotationPresent(Document.class)) {
                var resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);

                var indexOps = mongoTemplate.indexOps(clazz);
                resolver.resolveIndexFor(clazz).forEach(indexOps::ensureIndex);
            }
        }
    }

    log.info("Mongo InitIndicesAfterStartup take: {}", (System.currentTimeMillis() - init));
}

remember that var is only for java 11+

The final class, which uses Lombok

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver;
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;


@Slf4j
@RequiredArgsConstructor
@Configuration
public class MongoConfiguration {

private final MongoTemplate mongoTemplate;

private final MongoConverter mongoConverter;

@EventListener(ApplicationReadyEvent.class)
public void initIndicesAfterStartup() {

        log.info("Mongo InitIndicesAfterStartup init");
        var init = System.currentTimeMillis();

        var mappingContext = this.mongoConverter.getMappingContext();

        if (mappingContext instanceof MongoMappingContext) {
            MongoMappingContext mongoMappingContext = (MongoMappingContext) mappingContext;
            for (BasicMongoPersistentEntity<?> persistentEntity : mongoMappingContext.getPersistentEntities()) {
                var clazz = persistentEntity.getType();
                if (clazz.isAnnotationPresent(Document.class)) {
                    var resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);

                    var indexOps = mongoTemplate.indexOps(clazz);
                    resolver.resolveIndexFor(clazz).forEach(indexOps::ensureIndex);
                }
            }
        }

        log.info("Mongo InitIndicesAfterStartup take: {}", (System.currentTimeMillis() - init));
    }

}
like image 88
Juan Ibarra Avatar answered Nov 07 '22 04:11

Juan Ibarra


In addition to Juan Ibarra comment, adding this to application.yml file didnt work for me:

spring:
  data:
    mongodb:
      auto-index-creation: false

but overriding autoIndexCreation() in AbstractMongoClientConfiguration worked:

@Override
protected boolean autoIndexCreation() {
    return false;
}
like image 40
Piotr Filochowski Avatar answered Nov 07 '22 03:11

Piotr Filochowski