Skip to content

Instantly share code, notes, and snippets.

@jeffyuhaoliu
Created September 14, 2013 17:33
Show Gist options
  • Save jeffyuhaoliu/6563905 to your computer and use it in GitHub Desktop.
Save jeffyuhaoliu/6563905 to your computer and use it in GitHub Desktop.
Node.js MongoDB HW#5.3 - Use aggregation framework to calculate the class with the best average student performance. This involves calculating an average for each student in each class of all non-quiz assessments and then averaging those numbers to get a class average. To be clear, each student's average includes only exams and homework grades. …
use hw5
db.grades.aggregate([
{
$unwind: "$scores"
}
,{
$match: {
"scores.type": { $ne: "quiz" }
}
}
,{
$group: {
_id: {student: "$student_id", class: "$class_id"},
score: {
$avg: "$scores.score"
}
}
}
,{
$group: {
_id: "$_id.class",
avgScore: {
$avg: "$score"
}
}
}
,{
$sort: {
avgScore: 1
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment