-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhomework_2_2.py
36 lines (26 loc) · 925 Bytes
/
homework_2_2.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
# Write a program in the language of your choice that will remove
# the grade of type "homework" with the lowest score for each student
# from the dataset that you imported in HW 2.1. Since each document is one grade,
# it should remove one document per student.
import pymongo
import sys
# establish a connection to the database
connection = pymongo.Connection("mongodb://localhost", safe=True)
# get a handle to the school databse
db=connection.students
grades = db.grades
def find_and_remove():
query = {'type':'homework'}
try:
all_homework = grades.find(query)
sorted_homework = all_homework.sort([('student_id', pymongo.ASCENDING),('score',pymongo.ASCENDING)])
except:
print "Unexpected error:", sys.exc_info()[0]
sanity = 0
current_student = 0
for doc in sorted_homework:
if doc['student_id'] == current_student:
# print doc
grades.remove(doc)
current_student += 1
find_and_remove()