Je bent op de goede weg. In dit geval moet je $cond
. nesten operators doen dit als volgt:
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}}})
resultaten:
[ { _id: { Salary_Slab: 'Above 60K' }, count: 3 },
{ _id: { Salary_Slab: '30K-60K' }, count: 1 },
{ _id: { Salary_Slab: 'Under 30K' }, count: 2 } ]