如何通过对 mongo db 上的每个 属性 求和来将嵌套数组中的所有对象合并为一个对象?

How to merge all the objects in a nested array to one object by summing each property on mongo db?

给定第一个代码块是示例数据,第二个代码块是我想要的输出 在 MongoDB 中添加 Machine Stats 字段所需的查询是什么,这样我想要的输出就是这个(基本上添加 machine stats 中的所有字段数组)

{
    "date" : ISODate("2022-04-01T00:00:00.000Z"),
    "intervalName" : "Shift A",
    "operatorId" : "85875678",
    "__v" : 0,
    "clientId" : "ywegduywy",
    "createdAt" : ISODate("2022-05-05T07:33:08.183Z"),
    "deleted" : false,
    "machineStats" : [ 
        {
            "idleTime" : 10,
            "breaks" : 10,
            "loading" : 10,
            "unloading" : 10,
            "runtime" : 11,
            "total" : 100,
            "activity" : {}
        }, 
        {
            "idleTime" : 10,
            "breaks" : 10,
            "loading" : 10,
            "unloading" : 10,
            "runtime" : 10,
            "total" : 100,
            "activity" : {}
        }
    ],
    "plantId" : "AACCS3034M-SEZ-01",
    "totalActivity" : 10,
    "totalAll" : 100,
    "totalBreaks" : 10,
    "totalIdleTime" : 10,
    "totalLoadUnload" : 10,
    "totalRuntime" : 10,
    "updatedAt" : ISODate("2022-05-05T07:33:30.213Z")
}

我想要的期望输出(基本上是添加 machine stats 数组中除活动之外的所有字段)

{
    "date" : ISODate("2022-04-01T00:00:00.000Z"),
    "intervalName" : "Shift A",
    "operatorId" : "495632582487",
    "__v" : 0,
    "clientId" : "AACCS3034M",
    "createdAt" : ISODate("2022-05-05T07:33:08.183Z"),
    "deleted" : false,
    "machineStats" : [ 
        {
            "idleTime" : 20,
            "breaks" : 20,
            "loading" : 20,
            "unloading" :20,
            "runtime" : 21,
            "total" : 200,
            "activity" : {}
        }, 
       
    ],
    "plantId" : "AACCS3034M-SEZ-01",
    "totalActivity" : 10,
    "totalAll" : 100,
    "totalBreaks" : 10,
    "totalIdleTime" : 10,
    "totalLoadUnload" : 10,
    "totalRuntime" : 10,
    "updatedAt" : ISODate("2022-05-05T07:33:30.213Z")
}

您可以使用mapreduce方法在mongo查询中完成此类计算。

一种方法是使用 $reduce 遍历数组并将每个项目的数据添加到累积数据中,如下所示:

db.collection.aggregate([
  {
    $set: {
      machineStats: {
        $reduce: {
          input: "$machineStats",
          initialValue: {
            idleTime: 0,
            breaks: 0,
            loading: 0,
            unloading: 0,
            runtime: 0,
            total: 0
          },
          in: {
            idleTime: {$add: ["$$value.idleTime", "$$this.idleTime"]},
            breaks: {$add: ["$$value.breaks", "$$this.breaks"]},
            loading: {$add: ["$$value.loading", "$$this.loading"]},
            unloading: {$add: ["$$value.unloading", "$$this.unloading"]},
            total: {$add: ["$$value.total", "$$this.total"]},
            runtime: {$add: ["$$value.runtime", "$$this.runtime"]}
          }
        }
      }
    }
  }
])

Playground example.

另一种选择是使用 $unwind$group,但对于此特定请求的输出,它的效率应该较低。