【问题标题】:Mongodb find the highest two exams scoresMongodb查找最高的两个考试分数
【发布时间】:2022-04-09 23:38:00
【问题描述】:

我有一个学生收藏,如下所示: 如何为每个学生找到最多两个考试成绩

[
  {
    "_id" : ObjectId("61868aa03b2fe72b58c891a5"),
    "name" : "Max",
    "examScores" : [ 
        {
            "difficulty" : 4,
            "score" : 57.9
        }, 
        {
            "difficulty" : 6,
            "score" : 62.1
        }, 
        {
            "difficulty" : 3,
            "score" : 88.5
        }
    ]
},

{
    "_id" : ObjectId("61868aa03b2fe72b58c891a6"),
    "name" : "Manu",
    "examScores" : [ 
        {
            "difficulty" : 7,
            "score" : 52.1
        }, 
        {
            "difficulty" : 2,
            "score" : 74.3
        }, 
        {
            "difficulty" : 5,
            "score" : 53.1
        }
    ]
}
]

我想使用聚合返回两个最高的examScores.score 像这样:

[
 {
  name: "Max",
  maxExams: [88.5, 62.1]
 },
 {
  name: "Manu",
  maxExams: [74.3, 53.1]
 }
]

我尝试了聚合阶段 $project 和 $unwind 和 $sort 但他们都无法解决我的问题

【问题讨论】:

    标签: javascript node.js mongodb nosql


    【解决方案1】:

    这里是:

     mongos> db.s.aggregate([ 
            {$unwind:"$examScores"},
            {$sort:{"name":1,"examScores.score":-1}},
            {$group:{ _id:"$name" ,Scores:{$push:"$examScores.score"}  }}, 
            {$project:{_id:0,name:"$_id",maxExam:{$slice:["$Scores",2]}}}
            ])
    
           { "name" : "Manu", "maxExam" : [ 74.3, 53.1 ] }
           { "name" : "Max", "maxExam" : [ 88.5, 62.1 ] }
    mongos> 
    

    解释:

    1. 展开分数,以便日后对其进行排序。
    2. 按名称排序,分数从最大值到最小值
    3. 在新数组 Scores 中对分数进行分组
    4. 投影名称和 maxExam(从分数中切出前 2 个)

    【讨论】:

    • 非常感谢,一切正常
    【解决方案2】:

    查询

    • 您可以减少并仅保留最多 2 个分数
    • [-1,-1]开头如果分数>=第一个成员添加左(旧的右将被删除), else if score >= second member add right(旧的权限将被删除),否则什么也不做
    • 如果您想要超过 2-3
      • 如果 MongoDB 5 你可以使用 $setWindowFields
      • else unwind group slice 2 解决方案

    *这里不需要展开/分组/排序等,因为只有 2 个。

    PlayMongo

    aggregate(
    [{"$project": 
        {"_id": 0,
          "name": 1,
          "maxExams": 
          {"$reduce": 
            {"input": "$examScores",
              "initialValue": [-1, -1],
              "in": 
              {"$switch": 
                {"branches": 
                  [{"case": 
                      {"$gte": ["$$this.score", {"$arrayElemAt": ["$$value", 0]}]},
                      "then": 
                      {"$concatArrays": 
                        [["$$this.score"], [{"$arrayElemAt": ["$$value", 0]}]]}},
                    {"case": 
                      {"$gte": ["$$this.score", {"$arrayElemAt": ["$$value", 1]}]},
                      "then": 
                      {"$concatArrays": 
                        [[{"$arrayElemAt": ["$$value", 0]}], ["$$this.score"]]}}],
                  "default": "$$value"}}}}}}])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 2021-02-25
      • 1970-01-01
      • 2017-12-13
      • 2020-09-20
      相关资源
      最近更新 更多