0

I have in my aggregation pipeline a subarray structure like this:

  {
    "_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"
  },

In the $project stage, I'm passing through everything in the subarray 'data' as it currently exists:

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

How do I project the data such that it outputs voltage_a, voltage_b and voltage_c at the same level as "Voltage (V)" and "datetime", i.e. a flat structure?

chridam
  • 100,957
  • 23
  • 236
  • 235
ugotchi
  • 1,003
  • 1
  • 12
  • 32

1 Answers1

0

For a flat structure where you have an output with voltage_a, voltage_b and voltage_c at the same level as "Voltage (V)" and "datetime", run your pipeline as follows:

[
    '$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
    ]
]
chridam
  • 100,957
  • 23
  • 236
  • 235