-
-
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. …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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