mongodb 表里有userId,status两字段 其中status有0,1,2值
{userId:1, status: 0} {userId:1, status: 2} {userId:1, status: 2} {userId:2, status: 0} {userId:2, status: 1} {userId:2, status: 2}
我想根据用户分组查询,再筛选出每个用户不同status值出现次数,请问mongodb查询语句怎么写 ?
[ {userId:1, status_a: 1, status_b: 0, status_c: 2}, {userId:2, status_a: 1, status_b: 1, status_c: 1} ]
类似与mysql的
SELECT `userid`, COUNT( CASE WHEN `status` = '0' THEN 1 ELSE NULL END ), COUNT( CASE WHEN `status` = '1' THEN 1 ELSE NULL END ), COUNT( CASE WHEN `status` = '2' THEN 1 ELSE NULL END ) GROUP BY `userid`
相关分类