小编CoC*_*oCo的帖子

将字段添加到 mongo 聚合中对象数组中的每个对象

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

这是一个示例文档......

{
    calls: [
      {
        "name": "sam",
        "status": "scheduled"
      },
      {
        "name": "tom",
        "status": "cancelled"
      },
      {
        "name": "bob",
        "status": "scheduled"
      },
      
    ],
    "time": 1620095400000.0,
    "call_id": "ABCABCABC"
}
Run Code Online (Sandbox Code Playgroud)

所需文件如下:

[
  {
    "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
  }
]
Run Code Online (Sandbox Code Playgroud)

应将其call_id添加到数组中状态为“已计划”的所有对象。是否可以通过 mongo 聚合来做到这一点?我已经尝试过$addFields,但无法达到上述结果。提前致谢!

aggregation mongodb

3
推荐指数
1
解决办法
5591
查看次数

标签 统计

aggregation ×1

mongodb ×1