-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.js
172 lines (145 loc) · 4.5 KB
/
queries.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 1. How many Nobel Prizes have been awarded in the world, according to discipline?
var mapFunction = function() {
for(row of this.prizes) {
emit(row.category, {"year": row.year, "count": 1})
}
}
var reduceFunction = function(year, count_year) {
// Firstly, we are going to remove the arrays with duplicated objects, this happens when a nobel prize is awarded to more than one person.
count_year = [...new Set(count_year.map(s => JSON.stringify(s)))].map(s => JSON.parse(s))
count = 0
for(var row of count_year) {
count += row.count
}
return count;
}
db.nobel.mapReduce(
mapFunction, reduceFunction,
{
"out": "count_by_category"
}
)
db.count_by_category.find()
// 2. Which are the countries where most Nobel Prize winners were born?
var mapFunction = function() {
emit(this.bornCountry, 1)
}
var reduceFunction = function(country, array_count) {
return Array.sum(array_count)
}
db.nobel.mapReduce(
mapFunction, reduceFunction,
{
out: "count_by_country",
query:{ gender: {$ne: "org"}}
}
)
db.count_by_country.find().sort({value: -1})
// 3. Have women won more Nobel Prizes than men?
var mapFunction = function() {
emit(this.gender, 1)
}
var reduceFunction = function(gender, valuesArr) {
return Array.sum(valuesArr)
}
db.nobel.mapReduce(
mapFunction, reduceFunction,
{
"out": "count_by_gender"
}
)
db.count_by_gender.find()
// 4. In which disciplines do men and women mainly excel?
var mapFunction = function() {
for (row of this.prizes) {
emit(this.gender, row.category)
}
}
var reduceFunction = function(gender, categories) {
var count_physics = 0, count_medicine = 0, count_chemistry = 0, count_literature = 0, count_economics = 0, count_peace = 0
for (category of categories) {
if (category == "physics") {
count_physics += 1
}
if (category == "medicine") {
count_medicine += 1
}
if (category == "chemistry") {
count_chemistry += 1
}
if (category == "literature") {
count_literature += 1
}
if (category == "economics") {
count_economics += 1
}
if (category == "peace") {
count_peace += 1
}
}
return `Physics -> ${count_physics}, Medicine -> ${count_medicine}, Chemistry -> ${count_chemistry}, Literature -> ${count_literature}, Economics -> ${count_economics}, Peace -> ${count_peace}`
}
db.nobel.mapReduce(mapFunction, reduceFunction, {out: "gender_categories"})
db.gender_categories.find()
// 5. What are the top 3 South American countries with the most Nobel Prize winners?
var mapFunction = function() {
emit(this.bornCountry, 1)
}
var reduceFunction = function(country, count_countries) {
return Array.sum(count_countries)
}
db.nobel.mapReduce(
mapFunction, reduceFunction,
{
out: "ranking_sudamericanos",
query: {"bornCountryCode": {$in: ["AR", "CO", "CL", "PE", "UY", "PY", "BO", "VE", "EC", "BR"]}}
}
)
db.ranking_sudamericanos.find().sort({value: -1})
// 6. Which is the top 3 countries that have had the most female award winners?
var mapFunction = function() {
emit(this.bornCountry, 1)
}
var reduceFunction = function(country, count) {
return Array.sum(count)
}
db.nobel.mapReduce(
mapFunction, reduceFunction,
{
out: "females_by_country",
query: {gender: "female"}
}
)
db.females_by_country.find().sort({value: -1})
// 7. How many people have won more than one Nobel Prize?
var mapFunction = function() {
if(this.prizes.length > 1) {
emit(`${this.firstname} ${this.surname}`, this.prizes.length)
}
}
var reduceFunction = function(key, value) {
return `${value[0]} premios ganados`
}
db.nobel.mapReduce(mapFunction, reduceFunction, {out: "gte_one_prize", query:{ gender: {$ne: "org"}}})
db.gte_one_prize.find()
// 8. How many shared Nobel Prizes are there per category?
var mapFunction = function() {
for(prize of this.prizes) {
if(Number.parseInt(prize.share) > 1) {
emit(prize.category, {"year": prize.year, "count": 1})
}
}
}
var reduceFunction = function(category, year_count) {
//Remove duplicated objects
year_count = [...new Set(year_count.map(s => JSON.stringify(s)))].map(s => JSON.parse(s))
count = 0
for(info of year_count) {
count += info.count
}
return count
}
db.nobel.mapReduce(
mapFunction, reduceFunction, {out: "nobel_shared"}
)
db.nobel_shared.find()