0

I have an aggregation which doesn't work in mongodb and spring boot. I would be grateful if anyone could help me. Here is my ExplainDoc class:

@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; }

And here is my other class:

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

What I want to do is to make a text search and find all ExplainDocs which have the given text in their product_in_brief PROVIDED THAT their product has a specific category. In my search repository, I have an aggregation like the following:

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");
}

The code works now. However, I see it is so expensive to have the product object always as a nested document. How should I change the code if I want to use the product object as @DBRef? When I add the @DBRef, the code doesn't work anymore. I think the reason is that the product.category is not recognized anymore.

I hope somebody can help me.

user1419243
  • 1,655
  • 3
  • 19
  • 33

1 Answers1

0

There is nothing special about DBRef that magically makes it efficient. It is simply a label for the combination of collection name and an id. You still need to use aggregation pipeline to query data that uses DBRef in the same way you'd query if you didn't use DBRef.

D. SM
  • 13,584
  • 3
  • 12
  • 21