【问题标题】:Spring Mongodb aggregation doesn't work with DBRefSpring Mongodb 聚合不适用于 DBRef
【发布时间】:2021-04-20 14:05:24
【问题描述】:

我有一个在 mongodbspring 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;
}

我想做的是进行文本搜索并找到所有ExplainDocs 在其product_in_brief 中具有给定文本的所有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 不再被识别。

我希望有人可以帮助我。

【问题讨论】:

    标签: spring mongodb spring-boot aggregation dbref


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-04-03
      • 1970-01-01
      • 2017-08-03
      • 2013-06-24
      • 2020-12-30
      • 2015-10-31
      • 1970-01-01
      • 2020-12-22
      • 2018-08-17
      相关资源
      最近更新 更多