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`
1 回答
- 1 回答
- 0 关注
- 1980 浏览
添加回答
举报
0/150
提交
取消