Skip to content

Instantly share code, notes, and snippets.

@fbiville
Last active August 29, 2015 14:19
Show Gist options
  • Save fbiville/7810a1b1bd31048e912f to your computer and use it in GitHub Desktop.
Save fbiville/7810a1b1bd31048e912f to your computer and use it in GitHub Desktop.
M101J-W5
db.posts.aggregate([
{'$unwind':'$comments'},
{'$match': {
'comments.author': {
'$ne':'machine'
}
}},
{'$group': {
'_id':'$comments.author',
'count': {'$sum':1}
}},
{'$sort': {
'count':-1
}},
{'$limit':1}
])
db.zips.aggregate([
{'$match': {
'state': {
'$in': ['CA', 'NY']
}
}},
{'$group': {
'_id': {
state: '$state',
zip: '$city'
},
'population': {
'$sum': '$pop'
}
}},
{'$match': {
'population': {
'$gt':25000
}
}},
{'$group': {
'_id': {
state: '$state'
},
'population': {
'$avg': '$population'
}
}},
])
db.grades.aggregate([
{'$unwind':'$scores'},
{'$match': {
'scores.type': {
'$ne': 'quiz'
}
}},
{'$group': {
'_id': {
'class':'$class_id',
'student':'$student_id'
},
'averageScore': {
'$avg': '$scores.score'
}
}},
{'$group': {
'_id': '$_id.class',
'classAverageScore': {
'$avg':'$averageScore'
}
}},
{'$sort': {
'classAverageScore':-1
}},
{'$limit':1}
])
db.zips.aggregate([
{$project: {
"city": {$substr : ["$city", 0, 1]},
"population": "$pop"
}},
{"$match": {
"city": {
"$in": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
}
}},
{"$group": {
"_id": null,
"total_population": {
"$sum": "$population"
}
}}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment