Skip to content

Instantly share code, notes, and snippets.

@oreillyross
Created February 8, 2015 12:34
Show Gist options
  • Save oreillyross/5ae0079d71c80e1d700b to your computer and use it in GitHub Desktop.
Save oreillyross/5ae0079d71c80e1d700b to your computer and use it in GitHub Desktop.
H5.3 Mongo course. Aggregate and group function
db.grades.aggregate([
{$unwind: "$scores"},
{$match:
{'scores.type': "homework"}},
{$group:
{_id: {student_id: "$student_id",
class_id: "$class_id",
scorestype: "$scores.type",
},
avg_scores: {$avg:"$scores.score"}
}},
/* {$group:
{_id: {class: "$_id.class_id"},
average: {$avg: "$avg_scores"}}
},
{$sort: {'average': 1}} */
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment