MongoDB aggregation using Java… here is a solution to the problem.
MongoDB aggregation using Java
I
use GroupOperation in java to group fields and calculate values, but I don’t get the results I want.
I’m using Mongo Operation to connect to MongoDB via Java. I can group based on the field “Company”, but the count is always 0. I can’t figure out why.
GroupOperation
group=Aggregation.group("company").sum("company").as("count");
Aggregation aggregation=Aggregation.newAggregation(group);
AggregationResults<ResultMap> orderAggregate =
mongoOperations.aggregate(aggregation,EmpDesc.class,ResultMap.class);
orderAggregate.getMappedResults().forEach(s->System.out.println("^^
"+s.toString()));
Always get:
ResultMap [company=ABC, count=0]
ResultMap [company=XYZ, count=0]
Solution
It should be
Aggregation.group("company").count().as("count");