title: mongodb, how can I project or push root field on nested array using aggregate?
body: <p>I have the following example document with four nested arrays</p>
  
  <pre><code>{
      "_id" : ObjectId("5ed6bd9596908c36f4200980"),
      "attr2" : "hello",
      "attr3" : 1234,
      "attr1" : {
          "firstArray" : [
              {
                  "secondArray" : [
                      {
                          "secondAttr1" : "world",
                          "secondAttr2" : 456,
                          "secondAttr3" : [
                              {
                                  "finalArray" : [
                                      {
                                          "finalAttr1" : "alex",
                                          "finalAttr2" : 9876
                                      }
                                  ]
                              }
                          ]
                      }
                  ]
              }
          ]
      }
  
  }
  </code></pre>
  
  <p>and this aggregate query that finds through nested arrays, unwinding and creating new root for each array processed, returning elements from "finalArray"</p>
  
  <pre><code>db.AssetTest.aggregate(
  [
      {'$match':{'$and':[{'attr2': {'$eq':'hello'}}]}},
      {'$project': {'values': '$attr1.firstArray'}},
      {'$unwind':'$values'},
      {'$replaceRoot':{'newRoot':'$values'}},
  
      {'$project': {'values': {'$filter': {'input': '$secondArray','cond': {'$and':[{'$eq':['$$this.secondAttr1', 'world']}]}}}}},
      {'$unwind':'$values'},
      {'$replaceRoot':{'newRoot':'$values'}},
  
      {'$project': {'values': '$secondAttr3'}},
      {'$unwind':'$values'},
      {'$replaceRoot':{'newRoot':'$values'}},
  
      {'$project': {'values': {'$filter': {'input': '$finalArray','cond': {'$and':[{'$eq':['$$this.finalAttr1', 'alex']}]}}}}},
      {'$unwind':'$values'},
      {'$replaceRoot':{'newRoot':'$values'}}
  
      ]
  )
  </code></pre>
  
  <p>the result for this aggregate is</p>
  
  <pre><code>{
      "finalAttr1" : "alex",
      "finalAttr2" : 9876
  }
  </code></pre>
  
  <p>my question is, how can I project root field "attr2" through all aggregation stages so it will be on result array?</p>
  
  <pre><code>{
      "attr2"      : "hello",
      "finalAttr1" : "alex",
      "finalAttr2" : 9876
  }
  </code></pre>
  
  <p>I'm new to mongo and I'm completely lost on this so any help will be very appreciated.</p>
  
  <p>By the way I'm using mongo 3.4.15.</p>
  
  <p>Many Thanks!!</p>