Node with mongoose aggregate and sum nested fields
<p>When I do this query against my collection...</p>

<pre><code>models.Project.find(function(err, result) {
    //result = doc below
}).populate('media')
</code></pre>

<p>... I get this result:</p>

<pre><code>{ 
  _id: 57f36baa6cf34d079c8474a0,
  code: 'ZMIA',
  __v: 0,
  media:[
    { 
      _id: 57f36bb26cf34d079c847766,
      project_code: 'ZMIA',
      amount: 228,
      __v: 0 
    },
    { 
      _id: 57f36bb26cf34d079c84775c,
      project_code: 'ZMIA',
      amount: 250,
      __v: 0 
    } 
  ]
},
{ 
  _id: 57f36baa6cf34d079c8474a1,
  code: 'ZMJU',
  __v: 0,
  media: [] 
}
</code></pre>

<p><code>media</code> is a ref field. How can I aggregate the nested media objects (if present) to <code>$sum</code> the <code>amount</code> field and group the result by <code>project_code</code> ?</p>