简体   繁体   English

如何索引spring数据mongodb中的所有字段?

[英]How to index on all fields in spring data mongodb?

Say I have some collection called candidates.假设我有一些名为候选人的集合。 I want to create a text index on all fields inside this collection.我想在这个集合中的所有字段上创建一个text索引。 In mongo, I know that I can do it this way在 mongo 中,我知道我可以这样做

db.candidates.createIndex({"$**":"text"},{name:"TextIndex"}) 

Here is my java pojo or entity.这是我的 java pojo 或实体。

@Document(collection = "candidates")
public class Candidate {

    @Id
    private String id;
    private String firstName;
    private String lastName;

   // Other stuff: removed for brevity
}

Now how do I do db.candidates.createIndex({"$**":"text"},{name:"TextIndex"}) in java?现在我该怎么做db.candidates.createIndex({"$**":"text"},{name:"TextIndex"})在java中? That is how do I index an entire pojo or how do I do index all fields in my collection?那就是我如何索引整个 pojo 或者如何索引我的集合中的所有字段?

This question tried to do the same thing, but it does not have full details. 这个问题试图做同样的事情,但它没有完整的细节。

Also I looked at @Indexed annotation, however how can I use to index an entire collection since it can only be applied to a field?我还查看了@Indexed注释,但是我如何使用它来索引整个集合,因为它只能应用于一个字段?

With @TextIndexed you can at least index all the field you want to include in your index.使用@TextIndexed,您至少可以索引要包含在索引中的所有字段。 https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/index/TextIndexed.html https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/index/TextIndexed.html

Alternatively you can use the mongotemplate for this.或者,您可以为此使用 mongotemplate。 How to set @TextIndex name in an entity with Spring-Data-MongoDB 如何使用 Spring-Data-MongoDB 在实体中设置 @TextIndex 名称

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM