我试图了解在 Java Spring Boot 中定义模型时,@Indexed
和 @Field
这两个不同的注释有何不同。
public class Notation {
@Id
private String id;
@Field("value")
private String value;
@Field("description")
private String description;
@Field("frequency")
private int frequency;
}
public class Notation {
@Id
private String id;
@Indexed("value")
private String value;
@Indexed("description")
private String description;
@Field("frequency")
private int frequency;
}
我的用例是最终根据 value
和 description
字段从存储库中实现搜索,因此最好了解数据如何由两者组成,以及可以从这些注释中使用哪些选项。
最佳答案
@Indexed 注释将在 mongo 服务器中的该字段上添加索引。它需要一个可选的字符串参数,该参数将是索引名称,与字段名称无关。您应该只对那些将用于过滤文档的字段进行索引。
如果您想在 Java 代码和 MongoDB 集合中使用不同的名称,请使用 @Field。
例如。
@Field("desc")
private String description;
在这种情况下,在 MongoDB 集合中,您会发现字段名称为“desc”,而在 java 代码中您将引用它为“description”
@Field("description")
private String description;
上述情况下,不需要使用@Field注解
关于java - Spring MongoDB - @Indexed 和 @Field 注释之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57105949/