什么時候需要使用Group然后Count?
放在mongoDB種的數據總是有需要統計count的時候,但是通常的需求都不是統計所有的count,而是統計在一定條件下的count。
所以如果是手工完成這個工作是,先使用一定的條件對記錄完成過濾,然后統計count。
MongoDB總是如何做到這一點的呢?
如果只有一個條件?
1 db.getCollection('cat_rows').aggregate([ 2 { 3 "$group": {_id: "$dateStr", count: {"$sum": 1}} 4 } 5 ])
如果是多個條件呢?
1 db.Request.aggregate([ 2 {"$group" : {_id:{source:"$source",status:"$status"}, count:{$sum:1}}} 3 ])
