如何根据数组中的值对集合排序

2024-03-28 12:48:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为result的集合价值观:你知道吗

> db.result.findOne()
{
        "_id" : ObjectId("53b05264421aa97e980ba404"),
        "result" : [
                {
                        "attempted" : 49,
                        "subject_total_marks" : 50,
                        "score" : 15,
                        "correct_subject_answer" : 15,
                        "subject" : "Biology"
                },
                {
                        "attempted" : 30,
                        "subject_total_marks" : 30,
                        "score" : 4,
                        "correct_subject_answer" : 4,
                        "subject" : "Chemistry"
                },
                {
                        "attempted" : 20,
                        "subject_total_marks" : 20,
                        "score" : 7,
                        "correct_subject_answer" : 7,
                        "subject" : "Physics"
                },
                {
                        "attempted" : 99,
                        "correct_subject_answer" : 26,
                        "score" : 26,
                        "subject_total_marks" : 100,
                        "subject" : "Total"
                }
        ],
        "useruid" : NumberLong(548),
        "exam_code" : 301,
        "ess_time" : 1404062120
}

现在我想根据结果数组中“total”的“score”对集合进行排序。然后根据物理,数学的分数排序,然后根据化学,最后是生物学。你知道吗


Tags: answeriddb排序resulttotalsubjectscore
1条回答
网友
1楼 · 发布于 2024-03-28 12:48:27

正如您可能已经尝试过的那样,不能将数组中的特定项指定为“键”,以便通过简单的查找进行“排序”。为此,您将需要aggregate方法来获取要排序的键。你知道吗

db.exam.aggregate([

     # Unwind to de-normalize
     { "$unwind": "$result" },

     # Group back to the document and extract each score
     { "$group": {
         "_id": "$_id",
         "result": { "$push": "$result" },
         "useruid": { "$first": "$useruid" },
         "exam_code": { "$first": "$exam_code" },
         "ess_time": { "$first": "$ess_time" },
         "Total": { 
             "$max": {
                 "$cond": [
                     { "$eq": [ "$result.subject", "Total" ] },
                     "$result.score",
                     0
                 ]
             }
         },
         "Physics": { 
             "$max": {
                 "$cond": [
                     { "$eq": [ "$result.subject", "Physics" ] },
                     "$result.score",
                     0
                 ]
             }
         },
         "Mathematics": { 
             "$max": {
                 "$cond": [
                     { "$eq": [ "$result.subject", "Mathematics" ] },
                     "$result.score",
                     0
                 ]
             }
         },
         "Chemistry": { 
             "$max": {
                 "$cond": [
                     { "$eq": [ "$result.subject", "Chemistry" ] },
                     "$result.score",
                     0
                 ]
             }
         },
         "Biology": { 
             "$max": {
                 "$cond": [
                     { "$eq": [ "$result.subject", "Biology" ] },
                     "$result.score",
                     0
                 ]
             }
         }
     }},

     # Sort on those scores
     { "$sort": {
         "Total": -1,
         "Physics": -1,
         "Mathematics": -1,
         "Chemistry": -1,
         "Biology": -1
     }},

     # Project final wanted fields
     { "$project": {
         "result": 1,
         "useruid": 1,
         "exam_code": 1,
         "ess_time": 1
     }}
])

因此,在这里,在展开数组之后,您可以在^{}语句中使用^{}运算符“提取”匹配值。非规范化文档并不都具有相同的值,因为它们现在表示数组中的项,因此需要对它们进行测试。你知道吗

使用这些提取的键,您可以再次对整个文档进行排序,然后最终丢弃这些字段,因为您不再需要它们。你知道吗

相关问题 更多 >