查询controllerCode以及collectType为4,时间大于2018-05-24 16点以后的数据,按controllerCode分组,统计每个controllerCode对应多少数据,并按此数据排序:

db.dataCollection.aggregate(
	{
        $match: {"controllerCode":{$in:['153862',
'153867',
'153868',
'153869',
'153870',
'153871',
'153872',
'153873',
'153874',
'153875',
'153876',
'153877',
'153878',
'153879',
'153880',
'153881',
'153882',
'153883',
'153884',
'153885',
'153886',
'153887',
'153888',
'153889',
'153890',
'153891',
'153892',
'153893',
'153894',
'153895',
'153896',
'153897',
'153898',
'153900',
'153901',
'153902',
'153903',
'153904',
'153905',
'153906',
'153907',
'153908',
'153909',
'153910',
'153911',
'153912',
'153913',
'153914',
'153915',
'153916',
'153917',
'153918',
'153919',
'153920',
'153921',
'153922',
'153923',
'153924',
'153925',
'153926',
'153927',
'153928',
'153929',
'153930',
'153931',
'153932',
'153933',
'153935',
'153936',
'153937',
'153938',
'153939',
'153940',
'153861',
'153866',
'153899',
'153934',
'153941',
'153942',
'153943',
'153944',
'153945',
'153946',
'153857',
'153858',
'153859',
'153860',
'153863',
'153864',
'153865']},"collectType":4,"createDate":{"$gte" : ISODate("2018-05-24T08:00:00Z")}}
    },
    {
        $group: {
            _id: "$controllerCode",
            coun:{$sum:1},
        }
       
    },
    {$sort:{coun:-1}}
)
如果想对某列进行sum统计:在coun:{$sum:1}处改为->   mysum:{$sum:"$collectValue"},
如果时间在范围内:"createDate":{"$gte" : ISODate("2018-05-24T06:22:39Z"), "$lte" : ISODate("2018-05-24T09:22:00Z")}