Skip to content

Instantly share code, notes, and snippets.

@victorvzn
Created February 23, 2014 16:42
Show Gist options
  • Save victorvzn/9173780 to your computer and use it in GitHub Desktop.
Save victorvzn/9173780 to your computer and use it in GitHub Desktop.
Working with Bootle Framework and MongoDB by victorzn
from pymongo import MongoClient
import sys
client = MongoClient('localhost', 27017)
db = client.students
grades = db.grades
def find():
query = {'type':'homework'}
try:
cursor = grades.find(query, {'student_id':True,'score':True,'_id':True})
cursor = cursor.sort([('student_id',pymongo.DESCENDING),('score',pymongo.DESCENDING)])
except:
print "Unexpected error:", sys.exc_info()[0]
for doc in cursor:
student_id = doc["student_id"]
cursor_2 = grades.find({ 'type':'homework','student_id': student_id })
count_cursor_2 = cursor_2.count()
if count_cursor_2 == 2:
stud1 = cursor_2[0]
stud2 = cursor_2[1]
if stud1['score'] < stud2['score']:
print stud1['_id'],stud1['student_id'],stud1['score'],'(1)'
grades.remove({'_id': stud1['_id']})
else:
print stud2['_id'],stud2['student_id'],stud2['score'],'(2)'
grades.remove({'_id': stud2['_id']})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment