Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
792 views
in Technique[技术] by (71.8m points)

spring - 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 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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));
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...