Last active
June 27, 2018 15:02
-
-
Save codepainkiller/0006dc4f5754036abe78312f4cb157ab to your computer and use it in GitHub Desktop.
M101P - Week 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.posts.aggregate([ | |
{ | |
$unwind: "$comments" | |
}, | |
{ | |
$group: { | |
_id: "$comments.author", | |
count: {$sum: 1} | |
} | |
}, | |
{ | |
$sort: {count: -1} | |
} | |
]).pretty(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} | |
} | |
} | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | |
} | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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