任务是查找,排序和删除学生的类型':" homework"并使用MongoDB获得最低分。我也尝试使用toArray()
函数,但它给出了一个错误。现在我尝试将文档作为一些计数器移动,并删除分数最低的最后一个排序文档。
import pymongo
import sys
#establish a connection to the database
connection = pymongo.MongoClient("mongodb://localhost")
def delete_lowest_doc():
#get a handle to the students database
db=connection.students
grades = db.grades
try:
for i in range(0,grades.find().count()):
docs_1 = grades.find({'type':"homework", 'student_id':i}).sort(['score',-1])
counter_1 = grades.find({'type':"homework",'student_id':i})
counter_2 = counter_1.sort(['score',-1]).count()
while (counter_2>0):
doc = docs_1.next();
counter_2=counter_2-1;
grades.remove(doc)
except Exception as e:
print ("Unexpected error:", type(e), e)
delete_lowest_doc()
答案 0 :(得分:1)
collection.find
只需要一个位置参数,你就会给它两个。
更改您的通话,使它们如下所示:grades.find({"type": "homework", "student_id": i})
。