Was*_*ted 6 spring-data-mongodb spring-boot spring-mongodb
我提供了一个示例项目来阐明这个问题:https : //github.com/nmarquesantos/spring-mongodb-reactive-indexes
根据 spring mongo db 文档(https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mapping-usage):
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.
在我的 Player 类中,我们可以观察到 @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;
}
}`
Run Code Online (Sandbox Code Playgroud)
在我的应用程序类中,我插入 oneelement 以检查数据库是否已成功填充:
@PostConstruct
public void seedData() {
var player = new Player("Cristiano Ronaldo", "CR7");
playerRepository.save(player).subscribe();
}
Run Code Online (Sandbox Code Playgroud)
如果我在运行我的应用程序后检查 MongoDb,我可以看到成功创建的集合和元素。
不创建昵称的唯一索引。我只能看到为 @Id 属性创建的索引。我错过了什么吗?我是否误解了文档?
yej*_*lue 20
Spring Boot 2.3.0.RELEASE 附带的 Spring Data MongoDB 版本是 3.0.0.RELEASE。从 Spring Data MongoDB 3.0 开始,默认情况下禁用自动索引创建。
要启用自动索引创建,请设置spring.data.mongodb.auto-index-creation = true或者如果您有自定义 Mongo 配置,请覆盖该方法autoIndexCreation
@Configuration
public class CustomMongoConfig extends AbstractMongoClientConfiguration {
@Override
public boolean autoIndexCreation() {
return true;
}
// your other configuration
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4709 次 |
| 最近记录: |