Multiple $sum in $group in MongoDB
<p>Given this MongoDB collection:</p>

<pre><code>[
  { client: 'client1', type: 'Defect', time: 5 },
  { client: 'client1', type: 'Test', time: 5 },
  { client: 'client2', type: 'Management', time: 3 },
  { client: 'client2', type: 'Defect',     time: 3 },
  { client: 'client3', type: 'Test',     time: 4 }
]
</code></pre>

<p>I would like to get a sum of times from each issue_type like this:</p>

<pre><code>{
  client1:  { 'Defect': 5, 'Test': 5 },
  client2: { 'Management': 3, 'Defect': 3 },
  client3: { 'Test': 4 }
}
</code></pre>

<p>And I've been trying to do this using the aggregation framework (to replace an existing map/reduce) but have only been able to get as far as getting counts for the combinations like this:</p>

<pre><code>{ '_id': { client: 'Client1', class: 'Defect' },  sum: 5 }
{ '_id': { client: 'Client1', class: 'Test' }    count: 5 }
{ '_id': { client: 'Client2', class: 'Management' }, count: 3 } 
{ '_id': { client: 'Client2',     class: 'Defect' },  count: 3 }
{ '_id': { client: 'Client3',     class: 'Test' },  count: 4 }
</code></pre>

<p>Which is simple enough to reduce programmatically to the desired result but I was hoping to be able to leave that to MongoDB.</p>

<p>For any help that might be rendered many thanks in advance!</p>

<p>Edited</p>

<p>I'm adding this aggregation group</p>

<pre><code>db.getCollection('issues').aggregate(
    [
        {
            $group:
            {   
                _id: {component:"$client"},
                totalTime:{$sum: "$time"   }
            }
        }
    ]
)
</code></pre>