简体   繁体   English

Spring MongoDB - @Indexed 和 @Field 注释之间的区别

[英]Spring MongoDB - Difference between @Indexed and @Field annotations

I am trying to understand how the two different annotations of @Indexed and @Field differ while defining a model in Java Spring Boot.我试图了解在 Java Spring Boot 中定义模型时@Indexed@Field的两个不同注释@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;
}

My use case is to finally implement a search from the repository based on both value and description fields, so it would be good to get an idea of how the data is structured in the two and what are the various options one can use from these annotations.我的用例是最终基于valuedescription字段从存储库中实现搜索,因此最好了解数据在两者中的结构以及可以从这些注释中使用的各种选项是什么.

@Indexed annotation is will add an index that on that field in your mongo server. @Indexed 注释将在您的 mongo 服务器中的该字段上添加一个索引。 It takes an optional string parameter, which will be the index name and nothing to do with the field name.它需要一个可选的字符串参数,它将是索引名称,与字段名称无关。 You should have only those fields indexed which will be used for filtering out documents.您应该只索引那些将用于过滤文档的字段。

@Field is used if you want to have different names in your java code and MongoDB collection.如果您想在 Java 代码和 MongoDB 集合中使用不同的名称,则使用 @Field。

For eg.例如。

@Field("desc")
private String description;

In this case, in your MongoDB collection, you will find field name as "desc" while in your java code you will be referencing it as "description"在这种情况下,在您的 MongoDB 集合中,您会发现字段名称为“desc”,而在您的 Java 代码中,您将引用它为“描述”

@Field("description")
private String description;

In the above case, there is no need for using @Field annotation在上面的情况下,不需要使用@Field注解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring注释之间的区别 - Difference between Spring annotations Mongodb Spring数据:对索引字段的查询速度慢吗? - Mongodb Spring Data: Slow query on indexed field? 字段之间有什么区别:注释数组与单独的注释 - What is the difference between field: array of annotation vs separate annotations spring javaconfig,xml config和annotations之间的性能差异 - Performance difference between spring javaconfig, xml config and annotations Spring 编译时和运行时注解的区别 - Difference between compile time & run time annotations of Spring 在Spring框架中使用@Import和@ImportResource注释有什么区别? - What is the difference between the use of @Import and @ImportResource annotations in Spring framework? Spring 中的@Component、@Repository 和@Service 注解有什么区别? - What's the difference between @Component, @Repository & @Service annotations in Spring? 声明注释和类型注释之间的区别 - Difference between declaration annotations and type annotations Hibernate - 注释和公共注释之间的区别? - Hibernate - difference between annotations and commons-annotations? Spring Data MongoDB:当Document嵌入另一个时,如何忽略唯一索引字段? - Spring Data MongoDB: How ignore unique indexed field when Document is embedded in another one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM