Query in MongoDB for count rows (JasperSoft)

2019-09-06 17:46发布

I've an collection with 3 valuable fields (status, userid, sid).
How can i count rows for each smtpid and each status?
The status field can have values 0,1,2 or 3.

For example:
sid status userid
125 0 200
125 0 213
156 1 154
123 2 584

Purpose:
sid status count
125 0 2
156 1 1
123 2 1

Query for getting example:

{  
     runCommand : {  
    aggregate : 'sCollecion',  
    pipeline : [  
        { $match : { time : { '$gte' : '2014-01-01 00:00:00', '$lt' : '2014-01-01 02:00:00' } }  },  
        { $group : { _id : { StatusID : '$status', SID : '$sid' , UserID : '$userid'  } } }  
                    ]  
                }  
}

1条回答
Bombasti
2楼-- · 2019-09-06 17:55

You can simply add a count to your 'group' pipeline:

{
    $group : {
        _id : {
            StatusID : '$status',
            SID : '$sid',
            UserID : '$userid'
        },
        count : { $sum : 1 }
    }
}
查看更多
登录 后发表回答