Implementation of Aggregation Query on Mongodb

  node.js, question

Collection of articlesposts

ObjectId Post Category
1 post 1 a
2 post 2 b
3 post 3 b
4 post 4 a
5 post 5 c
6 post 6 b

Classified setcategories

ObjectId Category Name
1 a Category A
2 b Category B
3 c Category C

How to exportAll categories, and according to the number of articles under this category from more to less?

Mongo aggregation can only be operated on a single set, but yours is not complicated and can be easily implemented.

First passposts.aggregate([{$group:{_id:"$category",count:{$sum:1}}])Get the number of articles classified.
After that, if you need to display Category Name, you can go to Category inside to query.