Database Reference
In-Depth Information
"total" : 16601
},
...other output omitted...
{
"_id" : "E",
"total" : 21444
}
],
"ok" : 1
}
Thepreviousexampleusedthe
$sum
accumulator,butthereareanumberof
otheraccumulatorsthatcanbeusedincluding
$avg
tocomputetheaverage,
$min
to compute the minimum and
$max
to compute the maximum. The
_id
field can contain multiple values allowing for grouping on multiple
fields, but the output of the grouping is always unsorted. To sort the output,
a
$sort
operation can be applied to the output:
> db.aggtest.aggregate([{$group:{_id:"$first",
total:{$sum:"$count"}}},{$sort:{total:-1}}]);
{
"result" : [
{
"_id" : "H",
"total" : 27990
},
{
"_id" : "M",
"total" : 27188
},
{
"_id" : "L",
"total" : 25070
},
{
"_id" : "A",
"total" : 24148
},