-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhw5.js
88 lines (84 loc) · 1.06 KB
/
hw5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//hw 5.1
db.posts.aggregate([
{"$unwind":"$comments"},
{$group:
{
_id:"$comments.author",
num_comments:{$sum:1}
}
},
{$sort:
{
num_comments:1
}
}
])
// hw 5.2 - calculate ave population of cities in CA
//and NY when over 25,000
db.zips.aggregate([
{$match:
{
state:{$in:["NY","CA"]}
}
},
{$group:
{
_id:{state:"$state",city:"$city"},
"sum":{"$sum":"$pop"}
}
},
{$match:
{
sum:{"$gt":25000}
}
},
{$group:
{
_id:null,
"average_pop":{$avg:"$sum"}
}
}
])
// hw 5.3
db.grades.aggregate([
{$unwind:"$scores"},
{$match:
{
type:{$ne:"$quiz"}
}
},
{$group:
{
_id:"$class_id",
"avg_score":{$avg:"$scores.score"}
}
},
{$group:
{
_id:"$_id",
"avg_for_class":{$avg:"$avg_score"}
}
},
{$sort:{"avg_for_class":1}}
])
// hw 5.4
db.zips.aggregate([
{$project:
{
first_char: {$substr: ["$city",0,1]},
pop: "$pop",
zipcode: "$_id"
}
},
{$match:
{
first_char:{$in:["0","1","2","3","4","5","6","7","8","9"]}
}
},
{$group:
{
_id:0,
"total":{$sum:"$pop"},
}
}
])