vidushi dwivedi
vidushi dwivedi

Reputation: 41

Create criteria object using map in mongo

I have a map Map filterParams, whose key,value pair will be a part of a Criteria. How do I create the Criteria object.

Earlier I was using Query.addCriteria. However now I want a criteria object as I need to pass it to Aggregation.match() in mongo.

    filterParams.entrySet().forEach(e -> query.addCriteria(criteria.where(e.getKey()).is(e.getValue())));


        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria ),Aggregation.group("property_type.name").count().as("count"),
                Aggregation.project("property_type").andExclude("_id"));

Upvotes: 0

Views: 824

Answers (1)

Valijon
Valijon

Reputation: 13113

Try this one:

Criteria criteria = new Criteria();
filterParams.entrySet().forEach(e -> criteria.and(e.getKey()).is(e.getValue()));

Aggregation aggregation = Aggregation.newAggregation(
    Aggregation.match(criteria), 
    Aggregation.group("property_type.name").count().as("count"),
    Aggregation.project("property_type").andExclude("_id")
);

Upvotes: 2

Related Questions