2

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.

1
  • you have applied GroupOperation which return total count based on formed group. if you want total count remove GroupOperation Commented Jan 24, 2018 at 3:19

1 Answer 1

3

change this

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

to

CountOperation group = count().as("totalCnt");
Sign up to request clarification or add additional context in comments.

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.