I have this sample data,
{
_id: ObjectId("50b59cd75bed76f46522c34e"),
student_id: 0,
class_id: 2,
scores: [
{ type: 'exam', score: 57.92947112575566 },
{ type: 'quiz', score: 21.24542588206755 },
{ type: 'homework', score: 68.1956781058743 },
{ type: 'homework', score: 67.95019716560351 },
{ type: 'homework', score: 18.81037253352722 }
]
}
I want to aggregate this data to,
{
_id: ObjectId("50b59cd75bed76f46522c34e"),
student_id: 0,
class_id: 2,
scores: [
{ type: 'exam', score: 57.92947112575566 },
{ type: 'quiz', score: 21.24542588206755 },
{ type: 'homework', score: 51.6520826017 }
]
}
Where score from the same type will be calculated the average by sum all the score and divided by total number of same types.
How do I do this with aggregate in MongoDB? Thank You.