列出不重復(fù)數(shù)據(jù)項(xiàng)
db.getCollection('records').aggregate([
{$match: {is_job: true}},
])
按指定字段進(jìn)行統(tǒng)計(jì)
db.getCollection('records').aggregate([
{$match: {is_job: true}},
{$group: {_id: "$gname", count:{$sum: 1}}},
])
group里面定義了數(shù)據(jù)返回的字段打月,現(xiàn)在有 id(id 的意思是作為主鍵)和 count 字段
按指定字段排序
db.getCollection('records').aggregate([
{$match: {is_job: true}},
{$group: {_id: "$gname", count:{$sum: 1}}},
{ $sort: { count : -1 } }
])
擴(kuò)展閱讀
MongoDB對不重復(fù)數(shù)據(jù)分組進(jìn)行計(jì)數(shù)
https://blog.csdn.net/weixin_37732787/article/details/86568958MongoDB的skip,limit,sort執(zhí)行順序,以及aggregate
https://blog.csdn.net/jason_cuijiahui/article/details/80165367