0

mongodb我有一个在and中不起作用的聚合spring boot。如果有人可以帮助我,我将不胜感激。这是我的ExplainDoc课:

@Document(collection = "ExplainDoc")
public class ExplainDoc{

    @Id
    private String id;

    @TextIndexed(weight=3)
    private String product_in_brief;
    private Product product;
    
    @TextScore
    private Float textScore; }

这是我的另一堂课:

 @Document(collection = "product")
public class Product{   
    
    @Id 
    private String id;
    private String category;
}

我想做的是进行文本搜索并找到所有在其产品中具有ExplainDoc给定文本的 s,product_in_brief前提是他们的产品具有特定的类别。在我的搜索存储库中,我有一个如下聚合:

public List<MyAggrResults> searchBriefExplanations(String text, String category){
            
MatchOperation matchRegion = Aggregation.match(Criteria.where("product.category").is(category));
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny(text);
MatchOperation match = Aggregation.match(criteria);
GroupOperation group =   Aggregation.group("product.category").push("$$ROOT").as("myresults").sum("textScore").as("score");
ProjectionOperation project = Aggregation.project("product_in_brief", "product").andExpression("{$meta: \"textScore\"}").as("textScore");
}

该代码现在有效。但是,我认为将product对象始终作为嵌套文档非常昂贵。如果我想将product对象用作 ,我应该如何更改代码@DBRef?当我添加 时@DBRef,代码不再起作用。我认为原因是product.category不再被认可。

我希望有人能帮助我。

4

1 回答 1

0

DBRef 没有什么特别之处可以神奇地使它高效。它只是集合名称和 id 组合的标签。您仍然需要使用聚合管道来查询使用 DBRef 的数据,就像不使用 DBRef 时一样。

于 2021-01-15T20:07:12.313 回答