How can I translate the following Sql
query for Mongo
?:
select a,b,sum(c) csum from coll wh
Adding to previous answers, if you want to sort on the sum (the result of the aggregate) instead of on the actual column, you can do this:
db.your_collection.aggregate([
{
$group: {_id: "$your_document_name", count: {$sum: 1}}
},
{
$sort: {"count":-1}
}
])
This would be the equivalent of the following standard-SQL-ish syntax:
select col_a, count(col_a) as b
from table
group by col_a
order by b desc