MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

Harap gunakan 'MongoMappingContext#setAutoIndexCreation(boolean)' atau timpa 'MongoConfigurationSupport#autoIndexCreation()' agar eksplisit

Nonaktifkan pembuatan indeks otomatis di file application.properties

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

atau file application.yml

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

Buat kelas MongoConfiguration dengan anotasi @Configuration

Injeksi dependensi ini

private final MongoTemplate mongoTemplate;
private final MongoConverter mongoConverter;

dan tambahkan metode ini

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

ingat bahwa var hanya untuk java 11+

Kelas terakhir, yang menggunakan 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));
    }

}


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Properti tambahan validasi MongoDB jsonSchema

  2. Kemungkinan duplikat Mongo ObjectId dihasilkan dalam dua koleksi berbeda?

  3. Hubungkan ke Host mongodb dari wadah buruh pelabuhan

  4. Bagaimana cara mengembalikan hasil Mongoose dari metode find?

  5. Bagaimana cara mencapai rollback dalam transaksi di mongo?