You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MongoDB中遍历对象/数组并按section排序成绩

问题描述

给定如下格式的JSON数据:

{
    "someone": {
        "semesters":[
            {
                "semester": "Fall",
                "grades" : [
                    {
                        "exam" : "Midterm 1",
                        "sections" :[
                            {
                                "section" : "First Section",
                                "grade" : 30
                            },
                            {
                                "section" : "Second Section",
                                "grade" : 40
                            },
                            {
                                "section" : "Third Section",
                                "grade" : 50
                            }
                        ]
                    },
                    {
                        "exam" : "Midterm 2",
                        "sections" :[
                            {
                                "section" : "First Section",
                                "grade" : 12
                            },
                            {
                                "section" : "Second Section",
                                "grade" : 21
                            },
                            {
                                "section" : "Third Section",
                                "grade" : 56
                            }
                        ]
                    }
                ]
            }
        ]
    },
    "someone_else": {
        "semesters":[
            {
                "semester": "Fall",
                "grades" : [
                    {
                        "exam" : "Midterm 1",
                        "sections" :[
                            {
                                "section" : "First Section",
                                "grade" : 34
                            },
                            {
                                "section" : "Second Section",
                                "grade" : 3
                            },
                            {
                                "section" : "Third Section",
                                "grade" : 51
                            }
                        ]
                    },
                    {
                        "exam" : "Midterm 2",
                        "sections" :[
                            {
                                "section" : "First Section",
                                "grade" : 16
                            },
                            {
                                "section" : "Second Section",
                                "grade" : 80
                            },
                            {
                                "section" : "Third Section",
                                "grade" : 95
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

需要实现:遍历所有成绩数据,生成按指定section的成绩从高到低排序的列表,且可切换section查询对应排序结果。例如查询「First Section」时,期望输出:

{ name : someone_else, section: First Section, grade : 34 }, 
{ name : someone, section: First Section, grade : 30 }, 
{ name : someone_else, section: First Section, grade : 16 }, 
{ name : someone, section: First Section, grade : 12 }, 
解决方案

可以用Python实现该需求,核心思路是逐层遍历数据结构,筛选目标section的成绩条目后按分数降序排序。

# 原始数据
data = {
    "someone": {
        "semesters":[
            {
                "semester": "Fall",
                "grades" : [
                    {
                        "exam" : "Midterm 1",
                        "sections" :[
                            {"section" : "First Section", "grade" : 30},
                            {"section" : "Second Section", "grade" : 40},
                            {"section" : "Third Section", "grade" : 50}
                        ]
                    },
                    {
                        "exam" : "Midterm 2",
                        "sections" :[
                            {"section" : "First Section", "grade" : 12},
                            {"section" : "Second Section", "grade" : 21},
                            {"section" : "Third Section", "grade" : 56}
                        ]
                    }
                ]
            }
        ]
    },
    "someone_else": {
        "semesters":[
            {
                "semester": "Fall",
                "grades" : [
                    {
                        "exam" : "Midterm 1",
                        "sections" :[
                            {"section" : "First Section", "grade" : 34},
                            {"section" : "Second Section", "grade" : 3},
                            {"section" : "Third Section", "grade" : 51}
                        ]
                    },
                    {
                        "exam" : "Midterm 2",
                        "sections" :[
                            {"section" : "First Section", "grade" : 16},
                            {"section" : "Second Section", "grade" : 80},
                            {"section" : "Third Section", "grade" : 95}
                        ]
                    }
                ]
            }
        ]
    }
}

def get_sorted_section_grades(target_section):
    results = []
    # 遍历每个用户
    for name, user_info in data.items():
        # 遍历用户的学期
        for semester in user_info["semesters"]:
            # 遍历学期内的考试
            for exam in semester["grades"]:
                # 遍历考试的各个section
                for section in exam["sections"]:
                    if section["section"] == target_section:
                        results.append({
                            "name": name,
                            "section": target_section,
                            "grade": section["grade"]
                        })
    # 按成绩从高到低排序
    results.sort(key=lambda x: x["grade"], reverse=True)
    return results

# 示例:查询First Section的排序结果
target_section = "First Section"
sorted_results = get_sorted_section_grades(target_section)
# 格式化输出
for item in sorted_results:
    print(f"{{ name : {item['name']}, section: {item['section']}, grade : {item['grade']} }}, ")

执行上述代码后,输出与预期一致:

{ name : someone_else, section: First Section, grade : 34 }, 
{ name : someone, section: First Section, grade : 30 }, 
{ name : someone_else, section: First Section, grade : 16 }, 
{ name : someone, section: First Section, grade : 12 }, 

若要切换查询其他section,只需修改target_section变量的值(例如改为"Second Section"),即可获取对应section的成绩排序列表。

内容的提问来源于stack exchange,提问作者hat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 13:54:21