MongoDB聚合管道PHP - $ project上一阶段没有子数组

时间:2016-10-07 11:32:33

标签: php mongodb mongodb-query aggregation-framework mongodb-aggregation

我的聚合管道中有一个像这样的子阵列结构:

  {
    "_id": "london_10:2016-10-07 12",
    "data": {
      "voltage_a": 0.008888,
      "voltage_b": 0.008888,
      "voltage_c": 0.008888,
    },
    "Voltage (V)": 0.008888,
    "datetime": "2016-10-07T11:25:46"
  },

在$ project阶段,我通过子阵列中的所有内容'数据'因为它目前存在:

  [
    '$project' => [
      'data' => '$data',
      'Voltage (V)' => [
        '$divide' => [
          ['$add' => ['$data.voltage_a', '$data.voltage_b', '$data.voltage_c']]
        ],
      ],
     ]
   ]

如何投影数据,使其输出与"电压(V)"相同电压的电压_a,voltage_b和voltage_c。和" datetime",即一个扁平结构?

1 个答案:

答案 0 :(得分:0)

对于平面结构,您的输出voltage_avoltage_bvoltage_c"Voltage (V)""datetime"处于同一级别,请运行您的管道如下:

[
    '$project' => [
        'voltage_a' => '$data.voltage_a',
        'voltage_b' => '$data.voltage_b',
        'voltage_c' => '$data.voltage_c',
        'Voltage (V)' => [
            '$divide' => [
                [ '$add' => ['$data.voltage_a', '$data.voltage_b', '$data.voltage_c'] ]
            ],
        ],
        'datetime' => 1
    ]
]