Anda berada di jalur yang benar. Dalam hal ini Anda perlu membuat sarang $cond
operator seperti ini:
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}}})
hasil:
[ { _id: { Salary_Slab: 'Above 60K' }, count: 3 },
{ _id: { Salary_Slab: '30K-60K' }, count: 1 },
{ _id: { Salary_Slab: 'Under 30K' }, count: 2 } ]