-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudents_db.py
220 lines (159 loc) · 4.71 KB
/
students_db.py
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# -*- coding: utf-8 -*-
"""students_DB.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1XxpYEyLKcMs32H1NKyJoaJPMQR-JaQxq
Creating a database and then loading the student.json dataset.
Inserting the students record into the collection.
"""
import pymongo
import json
myclient = pymongo.MongoClient("mongodb://localhost:27017")
db = myclient["mydb"]
mycol = db["customers"]
with open("students.json") as students:
Data = [json.loads(i) for i in students]
mycol.insert_many(Data)
"""1) Find the student name who scored maximum scores in all (exam, quiz and homework)?
"""
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017")
db = myclient["mydb"]
mycol = db["customers"]
query = mycol.aggregate([
{"$unwind": "$scores"},
{"$group": {"_id": "$_id", "name": {"$first": "$name"}, "Total": {"$sum": "$scores.score"}}},
{"$sort": {"Total": -1}},
{"$limit": 1}
])
for i in query:
print(i)
"""2) Find students who scored below average in the exam and pass mark is 40%?"""
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017")
db = myclient["mydb"]
mycol = db["customers"]
query = {'scores.type': 'exam', 'scores.score': {'$gt': 40, '$lt': 60}}
myquery = mycol.aggregate([
{'$unwind': '$scores'},
{"$match": query}
])
for i in myquery:
print(i)
"""3) Find students who scored below pass mark and assigned them as fail, and above pass mark as pass in all the categories."""
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017")
db = myclient["mydb"]
mycol = db["customers"]
myquery = mycol.aggregate(
[{"$set":
{"scores":
{"$arrayToObject":
[{"$map":
{"input": "$scores",
"as": "s",
"in": {"k": "$$s.type", "v": "$$s.score"}}}]}}},
{"$project":
{
"_id": 1,
"name": 1,
"result": {
"$cond":
{"if": {"$and": [{"$gte": ["$scores.exam", 40]}, {"$gte": ["$scores.quiz", 40]},
{"$gte": ["$scores.homework", 40]}]
},
"then": "pass",
"else": "fail"
}
}
}
}
])
for i in myquery:
print(i)
"""4) Find the total and average of the exam, quiz and homework and store them in a separate collection.
"""
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017")
db = myclient["mydb"]
mycol = db["customers"]
total_avg = db.total_avg
myquery = mycol.aggregate([
{"$unwind": "$scores"},
{"$group":
{
"_id": "$_id",
"name": {"$first": "$name"}
,
"Total": {"$sum": "$scores.score"},
"Average": {"$avg": "$scores.score"}
}
},
{"$sort": {"_id": 1}}
])
myquery1 = []
for i in myquery:
myquery1.append(i)
print(i)
total_avg.insert_many(myquery1)
"""
5) Create a new collection which consists of students who scored below average and above 40% in all the categories."""
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017")
db = myclient["mydb"]
mycol = db["customers"]
Average_Candi = db.Average_Candi
myquery = mycol.aggregate(
[{"$match":
{"$expr":
{"$and":
[{"$gt": [{"$min": "$scores.score"}, 40]},
{"$lt": [{"$max": "$scores.score"}, 70]}
]
}
}
}])
myquery1 = []
for i in myquery:
myquery1.append(i)
print(i)
Average_Candi.insert_many(myquery1)
"""
6) Create a new collection which consists of students who scored below the fail mark in all the categories.
"""
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017")
db = myclient["mydb"]
mycol = db["customers"]
fail = db.fail
query = {}
data = mycol.aggregate(
[{"$match":
{"$expr":
{"$lt": [{"$max": "$scores.score"}, 40]}
}
}])
faila = []
for i in data:
faila.append(i)
print(i)
fail.insert_many(faila)
"""
7) Create a new collection which consists of students who scored above pass mark in all the categories."""
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017")
db = myclient["mydb"]
mycol = db["customers"]
passed = db.passed
query = {}
myquery = mycol.aggregate(
[{"$match":
{"$expr":
{"$gt": [{"$min": "$scores.score"}, 40]}
}
}])
passed1 = []
for i in myquery:
passed1.append(i)
print(i)
passed.insert_many(passed1)