Skip to content

Instantly share code, notes, and snippets.

@codepainkiller
Last active June 27, 2018 15:02
Show Gist options
  • Save codepainkiller/0006dc4f5754036abe78312f4cb157ab to your computer and use it in GitHub Desktop.
Save codepainkiller/0006dc4f5754036abe78312f4cb157ab to your computer and use it in GitHub Desktop.
M101P - Week 5
db.posts.aggregate([
{
$unwind: "$comments"
},
{
$group: {
_id: "$comments.author",
count: {$sum: 1}
}
},
{
$sort: {count: -1}
}
]).pretty();
db.zips.aggregate([
{
$match: {
$or: [ {state: "CA"}, {state: "NY"}]
}
},
{
$group: {
_id: {
city: "$city",
state: "$state"
},
pop: {$sum: "$pop"}
}
},
{
$match: {
pop: {$gt: 25000}
}
},
{
$group: {
_id: "$_id.state",
pop: {$avg: "$pop"}
}
},
{
$group: {
_id: null,
avg: {$avg: "$pop"}
}
}
]);
db.grades.aggregate([
{
$unwind: "$scores"
},
{
$match: {
"scores.type": {$ne: "quiz"}
}
},
{
$group: {
_id: {
class_id: "$class_id",
student_id: "$student_id"
},
avg_student: {$avg: "$scores.score"}
}
},
{
$group: {
_id: "$_id.class_id",
avg_class: {$avg: "$avg_student"}
}
},
{
$sort: {avg_class: -1}
}
]);
db.zips.aggregate([
{
$project: {
pop: 1,
first_char: {$substr : ["$city",0,1]}
}
},
{
$match: {
first_char: {$in: ["0","1","2","3","4","5","6","7","8","9"]}
}
},
{
$group: {
_id: null,
total: {$sum: "$pop"}
}
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment