本人写的第一个python程序:
# M101 Week2 - CRUD Homework 2.2
import pymongo
import sys
# establish a connection to the database
connection = pymongo.Connection("mongodb://localhost", safe=True)
db = connection.students
grades = db.grades
def hw2_2():
for i in range(0, 200):
query = {'type':'homework', 'student_id': i}
selector = {'student_id':1, '_id':1, 'score':1}
try:
cursor = grades.find( query, selector )
cursor = cursor.sort([('student_id',pymongo.ASCENDING),('score',pymongo.ASCENDING)])
except:
print "Unexpected error:", sys.exc_info()[0]
raise
for hw in cursor:
print "delete", hw
grades.remove(hw)
break
hw2_2()