복붙노트

[SPRING] @CompoundIndex가 Spring 데이터 MongoDB에서 작동하지 않습니다.

SPRING

@CompoundIndex가 Spring 데이터 MongoDB에서 작동하지 않습니다.

Spring Data MongoDB를 사용하는 응용 프로그램을 작성 중입니다. 내 모델 중 하나에서 복합 색인을 만들고 싶습니다. 상단에 @CompoundIndex 주석을 추가했습니다.

@Document
@CompoundIndexes({
    @CompoundIndex(name = "name_", def = "{ 'tenantId': 1, 'name': 1 }", unique = true)
})
public class MyModel {

}

그러나 색인은 작성되지 않습니다. 또한 클래스 위에 @CompoundIndex를 직접 배치하려고했습니다. 컬렉션에 여전히 색인이 없습니다. 다음과 같이 작성하면 동일한 색인 정의가 잘 작동합니다.

mongoTemplate.indexOps(MyModel.class).ensureIndex(new Index().named("name_").on("tenantId", Direction.ASC).on("name", Direction.ASC).unique());

주석의 주석 기반 정의를 사용하는 것을 선호합니다. 이 아이디어가 효과가없는 이유는 무엇입니까?

해결법

  1. ==============================

    1.이 경우 엔 인덱스 생성을 보장하기 위해 mongodb createIndex 메서드를 사용하고 싶습니다. 앱 모델 (이 경우 스프링 부트)에서 생성 한 경우에도 항상 이중 체크를 수행하고 수동으로 https : / /docs.mongodb.com/v3.2/reference/method/db.collection.createIndex/

    이 경우 엔 인덱스 생성을 보장하기 위해 mongodb createIndex 메서드를 사용하고 싶습니다. 앱 모델 (이 경우 스프링 부트)에서 생성 한 경우에도 항상 이중 체크를 수행하고 수동으로 https : / /docs.mongodb.com/v3.2/reference/method/db.collection.createIndex/

  2. from https://stackoverflow.com/questions/30774376/compoundindex-not-working-in-spring-data-mongodb by cc-by-sa and MIT license