I have a requirement, for say an Employee class which contains a field as joining date. So what I'm doing is
Query query = new Query();
query.addCriteria(Criteria.where("joiningdate").gte("").andOperator(where("joiningdate").lt("")));
List<Employeeemployee> trainings = mongoTemplate.find(query, Employee.class);
So I filtered out required employees based on the given date range. Now I want to apply aggregation on the returned value to calculate for example total salary.
I looked into a couple of examples and StackOverflow questions too but did not find what I was looking for. So any help?
The first step is to query the eligible data, and then summarize the total salary through the $sum function
Criteria criteria = Criteria.where("joiningdate").gt("").lt("");
// Just specify the query field
Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria), Aggregation.group("").sum("").as("totalSalary"));
AggregationResults<Map> results = mongoTemplate.aggregate(aggregation, "", Map.class);
Map map = results.getMappedResults().get(0);
System.out.println(map.get("totalSalary"));