0

I want to perform aggregation using multiple criteria. The problem is that I don't know how to pass multiple criteria. Do I declare multiple Match operation like below?

MatchOperation matchOperation1 = Aggregation.match(criteria);
MatchOperation matchOperation2 = Aggregation.match(criteria2);

And if yes, then how do I pass them to the aggregation method? I thought that it should be possible to create a MatchOperation that adheres in multiple criteria but I have not found such an example online.

5 Answers 5

3

Do I declare multiple Match operation like below?

MatchOperation matchOperation1 = Aggregation.match(criteria); 
MatchOperation matchOperation2 = Aggregation.match(criteria2);

The Criteria class has an and method, which allows combining two conditions. For example, consider the three documents:

{ _id: 1, size: 10, color: "blue" }
{ _id: 2, size: 12, color: "red" }
{ _id: 3, size: 8, color: "blue" }

The aggregation match stage is defined as follows:

Aggregation.match(Criteria.where("size").gt(new Integer(8))
                            .and("color").is("blue")
)

This returns the document with _id: 1.

Sign up to request clarification or add additional context in comments.

Comments

2

You can also use list of criteria in MatchOperation , when you want filter dynamically:

List<Criteria> criterias = new ArrayList<>();
for (String key : mongoRequest.getFilterQuery().keySet()) {
                Criteria ci= new Criteria();
                List<Object> listOfValues=new ArrayList<>();
                for(Object value:mongoRequest.getFilterQuery().get(key)){
                    listOfValues.add(value);
                }
                ci = Criteria.where(key).in(listOfValues);
                criterias.add(ci);
           }
MatchOperation match =new MatchOperation(!criterias.isEmpty()?new Criteria().andOperator(criterias.toArray(new Criteria[criterias.size()])):new Criteria());

you can also refer this link link.

Comments

1

You can try something like this

final Criteria firstMatchCriteria = Criteria.where("fielname").is(someValue)
final Criteria secondMatchCriteria = Criteria.where("fielname").is(someValue)
final Aggregation aggregation = Aggregation.newAggregation(        match(firstMatchCriteria),unwind("FIELD_"),match(secondMatchCriteria),project())

Comments

1

I have a better solution

Criteria result = criteria1.andOperator(criteria2, cri2teria,......)

Comments

0

That worked for me after all.

    Criteria criterias = new Criteria().andOperator(Criteria.where(Force.AMOUNT).gte(minForceAmount)
            .and(ForceType.TYPE).is(ForceTypeEnum.MAGNETIC_FORCE)
            .and("createdAt").gte(startDate).lte(endDate));
    MatchOperation matchOperation = Aggregation.match(criteria);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.