Skip to content

Instantly share code, notes, and snippets.

@paulobunga
Created June 20, 2022 19:24
Show Gist options
  • Save paulobunga/85111916cd6b752b1464cb5f2d33a239 to your computer and use it in GitHub Desktop.
Save paulobunga/85111916cd6b752b1464cb5f2d33a239 to your computer and use it in GitHub Desktop.
Top 10 Staff By Position
const agg = [
{
'$match': {
'positionInformation.positionStatus': 'Active'
}
}, {
'$unwind': {
'path': '$positionInformation'
}
}, {
'$project': {
'_id': 0,
'position': '$positionInformation.position',
'gender': {
'$cond': [
{
'$eq': [
'$gender', ''
]
}, 'NONE', '$gender'
]
}
}
}, {
'$group': {
'_id': '$position',
'male': {
'$sum': {
'$cond': [
{
'$eq': [
'MALE', '$gender'
]
}, 1, 0
]
}
},
'female': {
'$sum': {
'$cond': [
{
'$eq': [
'FEMALE', '$gender'
]
}, 1, 0
]
}
},
'none': {
'$sum': {
'$cond': [
{
'$eq': [
'NONE', '$gender'
]
}, 1, 0
]
}
},
'total': {
'$sum': 1
}
}
}, {
'$project': {
'_id': 0,
'position': '$_id',
'total': '$total',
'data': {
'male': '$male',
'female': '$female',
'none': '$none'
}
}
}, {
'$sort': {
'total': -1
}
}, {
'$limit': 10
}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment