MongoDB $group and explicit group formation with computed column

别来无恙 提交于 2019-12-06 05:33:01

You're on the right track. In this case you need to nest $cond operators like this:

db.UM.aggregate(
    { $project: {
        computed_column: {$cond: [{$gt: ['$Salary', 30000]},
            {$cond: [{$gt: ['$Salary', 60000]}, 'Above 60K', '30K-60K']},
            'Under 30K']}
    }},
    { $group: {_id: {Salary_Slab: '$computed_column'}, count: {$sum: 1}}})

results:

[ { _id: { Salary_Slab: 'Above 60K' }, count: 3 },
  { _id: { Salary_Slab: '30K-60K' }, count: 1 },
  { _id: { Salary_Slab: 'Under 30K' }, count: 2 } ]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!