Mongodb 向mongo聚合中对象数组中的每个对象添加字段

Mongodb 向mongo聚合中对象数组中的每个对象添加字段,mongodb,aggregation,Mongodb,Aggregation,我在根级别的模式中有一个字段,希望将它添加到数组中匹配条件的每个对象中 这是一个示例文档 { calls: [ { "name": "sam", "status": "scheduled" }, { "name": "tom", "status": &

我在根级别的模式中有一个字段,希望将它添加到数组中匹配条件的每个对象中

这是一个示例文档

{
    calls: [
      {
        "name": "sam",
        "status": "scheduled"
      },
      {
        "name": "tom",
        "status": "cancelled"
      },
      {
        "name": "bob",
        "status": "scheduled"
      },
      
    ],
    "time": 1620095400000.0,
    "call_id": "ABCABCABC"
}
所需文件如下:

[
  {
    "call_id": "ABCABCABC",
    "calls": [
      {
        "call_id": "ABCABCABC",
        "name": "sam",
        "status": "scheduled"
      },
      {
        "name": "tom",
        "status": "cancelled"
      },
      {
        "call_id": "ABCABCABC",
        "name": "bob",
        "status": "scheduled"
      }
    ],
    "time": 1.6200954e+12
  }
]
call\u id
应添加到数组中状态为“scheduled”的所有对象中。 是否可以通过mongo聚合实现这一点?我尝试了
$addFields
,但无法获得上述结果。
提前谢谢

下面是我将如何使用


非常感谢!它起作用了。
db.collection.aggregate([
  {
    "$addFields": {
      calls: {
        $map: {
          input: "$calls",
          as: "call",
          in: {
            $cond: [
              {
                $eq: [
                  "$$call.status",
                  "scheduled"
                ]
              },
              {
                "$mergeObjects": [
                  "$$call",
                  {
                    call_id: "$call_id"
                  }
                ]
              },
              "$$call"
            ]
          }
        }
      }
    }
  }
])