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

MongoDB聚合:基于条件为嵌套数组元素添加score字段

MongoDB聚合:为指定条件的数组元素添加字段

问题描述

需要在聚合查询中,仅当qualification.type等于"bachelor"时,为对应的qualification元素添加score字段。

样例数据

[
  {
    "status": "ACTIVE",
    "person": [
      {
        "name": "Jim",
        "age": "2",
        "qualification": [
          {
            "type": "education",
            "degree": {
              "name": "bachelor",
              "year": "2022"
            }
          },
          {
            "type": "certification",
            "degree": {
              "name": "aws",
              "year": "2021"
            }
          }
        ]
      }
    ]
  }
]

错误尝试

db.collection.aggregate([
  {
    $addFields: {
      "person.qualification.score": {
        $reduce: {
          input: "$person.qualification",
          initialValue: "",
          in: {
            $cond: [
              {
                "$eq": [
                  "$$this.type",
                  "bachelor"
                ]
              },
              "80",
              "$$REMOVE"
            ]
          }
        }
      }
    }
  }
])

问题分析

  1. $reduce使用错误:$reduce的作用是将数组迭代后合并为单个值,无法实现对数组中每个元素的单独修改。
  2. 嵌套数组处理逻辑错误:person和qualification都是数组,直接给person.qualification.score赋值会导致结构混乱,无法精准修改单个元素。
  3. 条件可能偏差:样例数据中qualification.type是"education",而degree.name才是"bachelor",如果实际需求是匹配学位名称,需要调整判断条件。

正确解决方案

场景1:匹配qualification.type为"bachelor"

db.collection.aggregate([
  {
    $addFields: {
      person: {
        $map: {
          input: "$person",
          as: "p",
          in: {
            ..."$$p",
            qualification: {
              $map: {
                input: "$$p.qualification",
                as: "q",
                in: {
                  ..."$$q",
                  score: {
                    $cond: [
                      { $eq: ["$$q.type", "bachelor"] },
                      "80",
                      "$$REMOVE"
                    ]
                  }
                }
              }
            }
          }
        }
      }
    }
  }
])

场景2:匹配qualification.degree.name为"bachelor"(符合样例数据)

db.collection.aggregate([
  {
    $addFields: {
      person: {
        $map: {
          input: "$person",
          as: "p",
          in: {
            ..."$$p",
            qualification: {
              $map: {
                input: "$$p.qualification",
                as: "q",
                in: {
                  ..."$$q",
                  score: {
                    $cond: [
                      { $eq: ["$$q.degree.name", "bachelor"] },
                      "80",
                      "$$REMOVE"
                    ]
                  }
                }
              }
            }
          }
        }
      }
    }
  }
])

逻辑说明

  • 用$map逐层遍历嵌套数组(先person,再qualification),确保每个元素都能被单独处理。
  • 使用扩展运算符...保留对象原有字段,避免修改时丢失数据。
  • 通过$cond判断条件:符合条件则添加score字段并赋值为"80",不符合则用$$REMOVE跳过该字段。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 09:18:25