Spring Boot 2.3.0 - MongoDB 라이브러리가 인덱스를 자동으로 생성하지 않음
저는 이 문제를 설명하기 위해 샘플 프로젝트를 제공했습니다: https://github.com/nmarquesantos/spring-mongodb-reactive-indexes
스프링 몽고드 문서(https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/ #mongodb-dll)에 따르면 다음과 같습니다.
the @Indexed annotation tells the mapping framework to call createIndex(…) on that property of your document, making searches faster. Automatic index creation is only done for types annotated with @Document.
플레이어 클래스에서는 @Document 및 @Indexed 주석을 모두 관찰할 수 있습니다.
@Document
public class Player {
@Id
private String id;
private String playerName;
@Indexed(name = "player_nickname_index", unique = true)
private String nickname;
public Player(String playerName, String nickname) {
this.id = UUID.randomUUID().toString();
this.playerName = playerName;
this.nickname = nickname;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}`
애플리케이션 클래스에서 데이터베이스가 올바르게 채워졌는지 확인하기 위해 한 가지 요소를 삽입합니다.
@PostConstruct
public void seedData() {
var player = new Player("Cristiano Ronaldo", "CR7");
playerRepository.save(player).subscribe();
}
애플리케이션 실행 후 MongoDb를 확인하면 성공적으로 생성된 컬렉션과 요소를 볼 수 있습니다.
닉네임에 대한 고유 인덱스가 생성되지 않습니다.@Id 속성에 대해 만들어진 인덱스만 볼 수 있습니다.제가 놓친 게 있나요?제가 문서를 잘못 해석했나요?
Spring Data MongoDB 버전은 Spring Boot 2.3.0과 함께 제공됩니다. RELEASE는 3.0.0입니다.풀어주다.Spring Data MongoDB 3.0 이후에는 자동 인덱스 생성이 기본적으로 비활성화되어 있습니다.
자동 색인 작성을 사용하려면, 설정spring.data.mongodb.auto-index-creation = true또는 사용자 지정 Mongo 구성이 있는 경우 메서드를 재정의합니다.
@Configuration
public class CustomMongoConfig extends AbstractMongoClientConfiguration {
@Override
public boolean autoIndexCreation() {
return true;
}
// your other configuration
}
스프링 부트 버전을 2.3.x로 업그레이드하고 구성 클래스에서 이 방법을 재정의하여 해결했을 때 이 문제에 직면했습니다(@yejianfengblue가 위에서 말한 것).
@Override
public boolean autoIndexCreation() {
return true;
}
언급URL : https://stackoverflow.com/questions/62208701/spring-boot-2-3-0-mongodb-library-does-not-create-indexes-automatically
'programing' 카테고리의 다른 글
| 열이 존재하지 않으면 null인 경우 columnValue를 선택합니다. (0) | 2023.07.03 |
|---|---|
| 열에서 구분된 문자열을 분할하고 새 행으로 삽입 (0) | 2023.07.03 |
| r을 사용한 폴더 관리 : 디렉터리가 있는지 확인하고 없으면 만듭니다. (0) | 2023.07.03 |
| 도커 합성으로 실행되는 스프링 부트 애플리케이션에 액세스할 수 없음 (0) | 2023.07.03 |
| 루비 보석 업그레이드 방법 (0) | 2023.07.03 |