MongoDB SELECT COUNT GROUP BY

后端 未结 7 2263
不知归路
不知归路 2020-11-22 15:24

I am playing around with MongoDB trying to figure out how to do a simple

SELECT province, COUNT(*) FROM contest GROUP BY province

But I can

7条回答
  •  一个人的身影
    2020-11-22 15:42

    Starting in MongoDB 3.4, you can use the $sortByCount aggregation.

    Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.

    https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/

    For example:

    db.contest.aggregate([
        { $sortByCount: "$province" }
    ]);
    

提交回复
热议问题