Lists non-duplicate data items

db.getCollection('records').aggregate([
    {$match: {is_job: true}},
])
Copy the code

Statistics are collected by the specified field

db.getCollection('records').aggregate([
    {$match: {is_job: true}},
    {$group: {_id: "$gname", count:{$sum: 1}}},
])
Copy the code

The group defines the fields that the data returns, and now has the ID (id means primary key) and count fields

Sort by the specified field

db.getCollection('records').aggregate([
    {$match: {is_job: true}},
    {$group: {_id: "$gname", count:{$sum: 1}}},
    { $sort: { count : -1 } }
])
Copy the code

Further reading

MongoDB counts groups of unduplicated data blog.csdn.net/weixin_3773…

Skip,limit,sort, and aggregate blog.csdn.net/jason_cuiji…