Skip to content

Instantly share code, notes, and snippets.

@sherlockholmes
Created October 23, 2015 19:02
Show Gist options
  • Select an option

  • Save sherlockholmes/5064d4197d03b0732cd6 to your computer and use it in GitHub Desktop.

Select an option

Save sherlockholmes/5064d4197d03b0732cd6 to your computer and use it in GitHub Desktop.
HOMEWORK 2 - WEEK 2 - M101P
import pymongo
import datetime
import sys
# establish a connection to the database
connection = pymongo.MongoClient("mongodb://localhost")
def find_student_data_and_delete_min(student_id):
# get a handle to the school database
db=connection.students
grades = db.grades
query = {'student_id':student_id, 'type':'homework'}
print "Searching for student data for student with id = ", student_id
try:
cursor = grades.find(query)
cursor = cursor.sort('score', pymongo.ASCENDING)
cursor = cursor.limit(1)
for doc in cursor:
grades.delete_many(doc)
except Exception as e:
print "Exception: ", type(e), e
i = 0
while (i < 200):
find_student_data_and_delete_min(i)
i = i + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment