james
james

Reputation: 275

spring mongodb aggregation sum counting

i am new to mongodb with spring boot. What i want is that how can i handle total count.

here is my spring code

Criteria creteria =Criteria.where("name").is("james"));
MatchOperation matchStage = Aggregation.match(creteria);
GroupOperation group = group("userId").count().as("totalCnt");


Aggregation aggregation = Aggregation.newAggregation(matchStage, group );

AggregationResults<Map> list= mongoTemplate.aggregate(aggregation,"listCollection", Map.class);



for(Map map:list){

    System.out.println("list :"+list);
}
===result===
list :{_id=111, totalCnt=1}
list :{_id=222, totalCnt=41}
list :{_id=333, totalCnt=41}

I want to calculate total count:83. How can i fix my code to get sum data. Please give me some hint.

Upvotes: 2

Views: 6487

Answers (1)

Saravana
Saravana

Reputation: 12817

change this

GroupOperation group = group("userId").count().as("totalCnt");

to

CountOperation group = count().as("totalCnt");

Upvotes: 3

Related Questions