Aggregation task - $group
A field-producing aggregation task that groups values for the specified fields.
The input for the $group task is a document
that defines one or more keys and a set of values to which a grouping
function can be applied. The keys are defined with the name_id.
To group on all documents, leave the key empty. For single keys a
simplified syntax can be used that assigns the source field to the _id.
Only inclusion is supported.
Example
Example 1
db.books.aggregate({$group: {_id: {}, "sum1": {"$sum": "$sales"}}})
Example 2
db.books.aggregate({$group: {_id: {"type": "$type"}, "sumValuePerType": {"$sum":"val"}}})