比较数组元素,去掉得分最低的

Compare array elements,remove the one with the lowest score

学校数据库中有 200 个文件。我必须删除每个具有 "type":"homework" 和最低分数的文档。

    {
        "_id" : 0,
        "name" : "aimee Zank",
        "scores" :
        [
            {
                "type" : "exam",
                "score" : 1.463179736705023
            },
            {
                "type" : "quiz",
                "score" : 11.78273309957772
            },
            {
                "type" : "homework",
                "score" : 6.676176060654615
            },
            {
                "type" : "homework",
                "score" : 35.8740349954354
            }
        ]
    }

比如这里

    {
        "type" : "homework",
        "score" : 6.676176060654615
    }

必须删除,因为分数 = 6.6 < 35.8

我把所有的文档排序成这样:

db.students.find({"scores.type":"homework"}).sort({"scores.score":1})

但我不知道如何删除得分最低的文档 type:homework??? 注意:如何通过不使用聚合方法来解决它?例如,通过排序然后更新。

这可以通过几个步骤完成。第一步是通过使用带有 $match, $unwind and $group 运算符的聚合框架来获取得分最低的文档列表,简化文档以找到每个文档的最低得分:

lowest_scores_docs = db.school.aggregate([ 
    { "$match": {"scores.type": "homework"} },
    { "$unwind": "$scores" },  { "$match": {"scores.type": "homework"} },
    { "$group": { "_id":"$_id", "lowest_score": {"$min": "$scores.score" } } } ] )

第二步是循环遍历上面的字典,在更新查询中使用$pull运算符从数组中移除元素如下:

for result in lowest_scores_docs["result"]:
    db.school.update({ "_id": result["_id"] }, 
        { "$pull": { "scores": { "score": result["lowest_score"] } } } )
import pymongo
import sys

# connnecto to the db on standard port
connection = pymongo.MongoClient("mongodb://localhost")

db = connection.school           # attach to db
students = db.students        # specify the colllection


try:

        cursor = students.find({})
        print(type(cursor))
        for doc in cursor:
            hw_scores = []
            for item in doc["scores"]:
                if item["type"] == "homework":
                    hw_scores.append(item["score"])
            hw_scores.sort()
            hw_min = hw_scores[0]
            #students.update({"_id": doc["_id"]},
             #           {"$pull":{"scores":{"score":hw_min}}})


except:
    print ("Error trying to read collection:" + sys.exc_info()[0])