I have data in the format given below.
{
"_id" : 19,
"name" : "Gisela Levin",
"scores" : [
{
"score" : 44.51211101958831,
"type" : "exam"
},
{
"score" : 0.6578497966368002,
"type" : "quiz"
},
{
"score" : 93.36341655949683,
"type" : "homework"
}
],
"studentType" : "poor"}
Now i want to find the average of exam score for each studentType and i have written a query
db.student.aggregate(
{$project : {_id : 0, studentType : 1, "scores.0.score" : 1}},
{$group : {_id : "$studentType", AverageMarks : {$avg : "$scores.0.score"}}}
)
But i am getting this answer
{ "_id" : "good", "AverageMarks" : null }
{ "_id" : "avg", "AverageMarks" : null } { "_id" : "poor", "AverageMarks" : null }