Mongodb group by queries

mongodbmongodb-3.0

Lets say one of my items in a collection looks like that:

{
User:123
Grade : 100
}

I want to sum up the grades for each student so i have this output:

123: 100, 445: 30 etc

Im normal sql, its the equivalent of a group by + sum() query. How can i do it with mongo?

Best Answer

You will have to use the mongodb aggregation framework.

Check this example: https://docs.mongodb.org/manual/tutorial/aggregation-zip-code-data-set/