Skip to content

Instantly share code, notes, and snippets.

@lengpiseth
Forked from mrmarcondes/homework5.js
Created August 31, 2013 03:42
Show Gist options
  • Save lengpiseth/6396095 to your computer and use it in GitHub Desktop.
Save lengpiseth/6396095 to your computer and use it in GitHub Desktop.
// homework 5.1
db.posts.aggregate([
{$project: {"_id": 0,
"comments.author": 1
}},
{$unwind: "$comments"},
{$group: {"_id": "$comments.author",
sum: {"$sum": 1}
}},
{$sort: {"sum": -1}},
])
// homework 5.2
db.zips.aggregate([
{$group:{
"_id": {state: "$state",
city: "$city"},
pop: {"$sum": "$pop"}
}},
{$match:{"pop":{"$gt":25000}, "_id.state": {"$in": ["CA", "NY"]}}},
{$group:{
"_id":null,
avg: {"$avg": "$pop"}
}}
])
// homework 5.3
db.grades.aggregate([
{$unwind:"$scores"},
{$match:{"scores.type":{"$ne": "quiz"}}},
{$group:{"_id": {class_id: "$class_id",
student_id: "$student_id"},
avg_per_student: {"$avg": "$scores.score"}
}},
{$group:{
"_id": "$_id.class_id",
avg: {"$avg": "$avg_per_student"}
}},
{$sort: {"avg": -1}}
])
// homework 5.4
db.zips.aggregate([
{$project:
{
first_char: {$substr : ["$city",0,1]},
pop:1
}},
{$match:{"first_char":{"$regex": "[0-9]"}}},
{$group:{
"_id":null,
sum: {"$sum": "$pop"}
}}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment