For example, a user query the collection, female gender, count the number of the same age
db.User.group({
key: {
'Age' : 1
},
cond: {"sex":1},
reduce: function ( curr, result ) {
result.total += 1;
},
initial: {
total : 0
}
}
)
Equivalent to the sql
select COUNT(0) as total,Age from User where Sex = 1 GROUP BY Age
The simple example more information view the document
https://docs.mongodb.com/manual/reference/method/db.collection.group/#db.collection.group