I would like to get count of each unique column field by date from mongodb data. Please see the example below.
Here I need result to display Monthly data of each subject with total count of each subject. I have tried grouping but couldn't get the actual output needed.
Student collection:
[{
_id: uniqueId,
Date: '201507', // Year and month
Subject: 'Maths',
},
{
_id: uniqueId,
Date: '201507', // Year and month
Subject: 'Physics'
},
{
_id: uniqueId,
Date: '201508', // Year and month
Subject: 'Maths'
},
{
_id: uniqueId,
Date: '201507', // Year and month
Subject: 'Maths'
}
]
The output will be
[
{
Date: 201507,
Subject: {
Maths: 2, // count
Physics: 1
}
},
{
Date: 201508,
Subject: {
Maths: 1, // count
}
}
]