fir*_*l.1 2 java spring mongodb spring-boot
我试图了解在 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;
}
Run Code Online (Sandbox Code Playgroud)
public class Notation {
@Id
private String id;
@Indexed("value")
private String value;
@Indexed("description")
private String description;
@Field("frequency")
private int frequency;
}
Run Code Online (Sandbox Code Playgroud)
我的用例是最终基于 和value字段从存储库中实现搜索description,因此最好了解两者中数据的结构以及可以从这些注释中使用的各种选项。
@Indexed 注释将在 mongo 服务器中的该字段上添加索引。它需要一个可选的字符串参数,该参数将是索引名称,与字段名称无关。您应该只对那些将用于过滤文档的字段进行索引。
如果您想在 Java 代码和 MongoDB 集合中使用不同的名称,请使用 @Field。
例如。
@Field("desc")
private String description;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,在 MongoDB 集合中,您会发现字段名称为“desc”,而在 java 代码中您将引用它为“description”
@Field("description")
private String description;
Run Code Online (Sandbox Code Playgroud)
在上述情况下,不需要使用@Field注解
| 归档时间: |
|
| 查看次数: |
4511 次 |
| 最近记录: |