Skip to content

Instantly share code, notes, and snippets.

@hperantunes
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hperantunes/23574b84d68af97a4d46 to your computer and use it in GitHub Desktop.
Save hperantunes/23574b84d68af97a4d46 to your computer and use it in GitHub Desktop.
db.grades.aggregate([
{$unwind: '$scores'},
{$match: {'scores.type': {$ne: 'quiz'}}},
{$project: {_id: 0, class: '$class_id', student: '$student_id', score: '$scores.score'}},
{$group: {_id: {class: '$class', student: '$student'}, student_avg: {$avg: '$score'}}},
{$group: {_id: {class: '$_id.class'}, avg: {$avg: '$student_avg'}}},
{$project: {_id: 0, class: '$_id.class', avg: 1}},
{$sort: {avg: -1}}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment