title: MongoDB aggregate sum of each element
body: <p>I have the following document: </p>
  
  <pre><code>    [
    {
      "name": "firstEntry",
      "values": [
        1,
        3
      ]
    },
    {
      "name": "secondEntry",
      "values": [
        9,
        5
      ]
    }
  ]
  </code></pre>
  
  <p>My final objective is the get sum of each element. For example: </p>
  
  <pre><code>final:[10,8]
  </code></pre>
  
  <p>So basically each element in the "final" array with n elements is the sum of each element in the "value" array with n elements.</p>
  
  <ul>
  <li>final[0] = (document1)values[0]+(document2)values[0] </li>
  <li>final[1] = (document1)values[1]+(document2)values[1]</li>
  </ul>
  
  <p>I have tried the following but keep getting stuck after "unwind" stage and don't know how to proceed. </p>
  
  <pre><code>db.entries.aggregate ({
      "$unwind": { path: "$values" }
  },{
  "$group" : {
              "_id" : {
                  "index" : "$arrayIndex",
              }
     }});
  </code></pre>
  
  <p>Any help is appreciated. </p>