簡體   English   中英

如何索引spring數據mongodb中的所有字段?

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

假設我有一些名為候選人的集合。 我想在這個集合中的所有字段上創建一個text索引。 在 mongo 中,我知道我可以這樣做

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

這是我的 java pojo 或實體。

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

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

   // Other stuff: removed for brevity
}

現在我該怎么做db.candidates.createIndex({"$**":"text"},{name:"TextIndex"})在java中? 那就是我如何索引整個 pojo 或者如何索引我的集合中的所有字段?

這個問題試圖做同樣的事情,但它沒有完整的細節。

我還查看了@Indexed注釋,但是我如何使用它來索引整個集合,因為它只能應用於一個字段?

使用@TextIndexed,您至少可以索引要包含在索引中的所有字段。 https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/index/TextIndexed.html

或者,您可以為此使用 mongotemplate。 如何使用 Spring-Data-MongoDB 在實體中設置 @TextIndex 名稱

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM