I am having an array of objects:
[
{
medicationDetails: {
category: 'Asthma',
subCategory: '',
providedBy: 'Doctor'
}
},
{
medicationDetails: {
category: 'Diabetes',
subCategory: 'Oral',
providedBy: 'Nurse'
}
},
{
medicationDetails: {
category: 'Asthma',
subCategory: ''
providedBy: 'Doctor'
}
},
{
medicationDetails: {
category: 'Diabetes',
subCategory: 'Insulin',
providedBy: 'Doctor'
}
}
]
Some medication will have sub-category and some doesn't.
Need to group based on 'category', 'sub-category' and 'providedBy' and get the count like this:
[
{
medicationDetails: [
{
name: 'Asthma',
providedByDoctorCount: 2,
providedByNurseCount: 0
},
{
name: 'Oral',
providedByDoctorCount: 0,
providedByNurseCount: 1
},
{
name: 'Insulin',
providedByDoctorCount: 1,
providedByNurseCount: 0
}
]
}
]
I was tried with $group and $count but not getting the expected result. Need some valuable help.