首页 > IT职业 > IT图书 > Mongodb查询不重复数据项出现次数并排序

db.getCollection('records').aggregate([
{$match: {is_job: true}},
])
db.getCollection('records').aggregate([
{$match: {is_job: true}},
{$group: {_id: "$gname", count:{$sum: 1}}},
])
group里面定义了数据返回的字段,现在有 id(id 的意思是作为主键)和 count 字段
db.getCollection('records').aggregate([
{$match: {is_job: true}},
{$group: {_id: "$gname", count:{$sum: 1}}},
{ $sort: { count : -1 } }
])
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2687/viewspace-2827053/ ,如需转载,请注明出处,否则将追究法律责任。