title: Flatten MongoDB nested array in aggregation
body: <p>I have documents like this:</p>
  
  <pre><code>{
      "many" : {},
      "other" : {},
      "fields" : {},
      "phases" : [ 
          {
              "type" : 10,
              "states" : [ 
                  {
                      "type" : 10,
                      "time" : ISODate("2018-04-25T13:06:42.990+02:00")
                  }, 
                  {
                      "type" : 20,
                      "time" : ISODate("2018-04-25T13:26:12.122+02:00")
                  }, 
                  {
                      "type" : 30,
                      "time" : ISODate("2018-04-25T13:26:30.124+02:00")
                  }
              ]
          }, 
          {
              "type" : 20,
              "states" : [ 
                  {
                      "type" : 10,
                      "time" : ISODate("2018-04-25T13:27:58.201+02:00")
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>Within an aggregation, I'm trying to flatten the <code>states</code> including the parent's <code>type</code> like this (desired output):</p>
  
  <pre><code>"states" : [ 
      {
          "phase": 10,
          "type" : 10,
          "time" : ISODate("2018-04-25T13:06:42.990+02:00")
      }, 
      {
          "phase": 10,
          "type" : 20,
          "time" : ISODate("2018-04-25T13:26:12.122+02:00")
      }, 
      {
          "phase": 10,
          "type" : 30,
          "time" : ISODate("2018-04-25T13:26:30.124+02:00")
      }, 
      {
          "phase": 20,
          "type" : 10,
          "time" : ISODate("2018-04-25T13:27:58.201+02:00")
      }
  ]
  </code></pre>
  
  <p>I already accomplished the additional <code>states</code> field without the <code>phase</code> field with this aggregation:</p>
  
  <pre><code>db.docs.aggregate([
  {
      $addFields: {
          states: {
              $reduce: {
                    input: "$phases",
                    initialValue: [],
                    in: { $concatArrays: ["$$value", "$$this.states"] }
                 }
             }
      }
  }
  ])
  </code></pre>
  
  <p>The 'many other fields' should be preserved, so I believe grouping is not an option.<br>
  MongoDB version is 3.4.</p>
  
  <p>I tried many things without result. I wonder if and how this is possible.</p>