yes 1 group by can do that for all groups.
group by seperates the collections in many groups and in each group separatly apply the accumulator (here find the total members of the group)
Example data (20 students with credits 0 to 2 ) (you can have any students and credits 0 131)
{:student_name "name0", :credit 2}
{:student_name "name1", :credit 2}
{:student_name "name2", :credit 1}
{:student_name "name3", :credit 0}
{:student_name "name4", :credit 2}
{:student_name "name5", :credit 0}
{:student_name "name6", :credit 1}
{:student_name "name7", :credit 1}
{:student_name "name8", :credit 2}
{:student_name "name9", :credit 0}
{:student_name "name10", :credit 2}
{:student_name "name11", :credit 0}
{:student_name "name12", :credit 1}
{:student_name "name13", :credit 0}
{:student_name "name14", :credit 1}
{:student_name "name15", :credit 1}
{:student_name "name16", :credit 2}
{:student_name "name17", :credit 1}
{:student_name "name18", :credit 2}
{:student_name "name19", :credit 0}
1 group by credit ,and i get 3 documents
{total_students 7, credit 1}
{total_students 7, credit 2}
{total_students 6, credit 0}
You only need this,it will return a cursor with the 131 documents.
each document = 1 credit value , with the “count” the number of students
Try it if you can.
collection.aggregate(
Arrays.asList(
Aggregates.group("credit", Accumulators.sum("count", 1)));