title: How to combine two separate aggregations together in same result
body: <p>I want to take two aggregations and combine them together. "Blakes Seven" kindly helped me with a couple of queries to retrieve first and second array element values but I need the two together.</p>
  
  <p>First one:</p>
  
  <pre><code>db.getCollection('anothertest').aggregate([
  { "$unwind": "$a" },
  { "$group": {
      "_id": "$_id",
      "first": { "$first": "$a" }
  }}
  ])
  </code></pre>
  
  <p>Give a result of:</p>
  
  <pre><code>{
  "result" : [ 
      {
          "_id" : ObjectId("565cc5261506995581569439"),
          "first" : 4
      }
  ],
  "ok" : 1.0000000000000000
  }
  </code></pre>
  
  <p>Second one:</p>
  
  <pre><code>db.getCollection('anothertest').aggregate([
  { "$unwind": "$a" },
  // Keeps the first element
  { "$group": {
      "_id": "$_id",
      "first": { "$first": "$a" },
      "a": { "$push": "$a" }
   }},
  { "$unwind": "$a" }
  ,
  // Removes the first element
  { "$redact": {
  "$cond":{
  "if": { "$ne": [ "$first", "$a" ] },
  "then": "$$KEEP",
    "else": "$$PRUNE"
  }
  }}
  </code></pre>
  
  <p>Gives the result:</p>
  
  <pre><code>{
  "result" : [ 
      {
          "_id" : ObjectId("565cc5261506995581569439"),
          "second" : 2
      }
  ],
  "ok" : 1.0000000000000000
  }
  </code></pre>
  
  <p>What I need is the result of:</p>
  
  <pre><code>{
  "result" : [ 
      {
          "_id" : ObjectId("565cc5261506995581569439"),
          "first" : 4
          "second" : 2
      }
  ],
  "ok" : 1.0000000000000000
  }
  </code></pre>
  
  <p>In addition to this, the document it is based on is just an example where the values sit as elements within an array. How would I use this on a nested array? If this is all to complicated for Aggregation, maybe there is an easier way to achieve this using Map Reduce? Thanks for any help provided.</p>
  
  <p>The document for the above is below:</p>
  
  <pre><code>{
  "_id" : ObjectId("565cc5261506995581569439"),
  "a" : [ 
      4, 
      2, 
      8, 
      71, 
      21
  ]
  }
  </code></pre>
  
  <p><strong>UPDATE TO MY ORIGINAL POST</strong>
  I have now managed to take the awesome script and make it work for my document which has nested arrays - up to a point.</p>
  
  <pre><code>{ "$match": {objectOriginAPI : "Profit &amp; Loss"}},
  { "$unwind": "$objectRawOriginData.Reports" },
          { "$unwind": "$objectRawOriginData.Reports.Rows" },
          { "$unwind": "$objectRawOriginData.Reports.Rows.Rows" },
          { "$unwind": "$objectRawOriginData.Reports.Rows.Rows.Cells" },
  { "$group": {
      "_id": "$_id",
      "first": { "$first": "$objectRawOriginData.Reports.Rows.Rows.Cells.Value" },
      "a": { "$push": "$objectRawOriginData.Reports.Rows.Rows.Cells.Value" }
  }},
  { "$unwind": "$a" },
  {"$skip":1},
  { "$group": {
      "_id": "$_id",
      "BalanceName": { "$first": "$first" },
      "BalanceValue": { "$first":      "$objectRawOriginData.Reports.Rows.Rows.Cells.Value" },
  }}
  ])
  </code></pre>
  
  <p>Actually, accidentally I forgot to update the second $unwind so it remained as "$a". and what happened was I got the following result:</p>
  
  <pre><code>{
  "result" : [ 
      {
          "_id" : ObjectId("564d12da1506995581569428"),
          "BalanceName" : "Sales",
          "BalanceValue" : null
      }
  ],
  "ok" : 1.0000000000000000
  }
  </code></pre>
  
  <p>But when I changed the $unwind to match the $unwind for the first value, I get no results at all. I cannot understand why, any ideas? Thanks, Matt</p>