title: Mongodb group by key
body: <p>I have the following recordset as result of a find:</p>
  
  <pre><code>[{ 'programming': 7 },
  { 'programming': 9 },
  { 'programming': 6 },
  { 'programming': 10 },
  { 'music': 1 },
  { 'music': 2 },
  { 'music': 3 },
  { 'music': 4 }]
  </code></pre>
  
  <p>I want to group them by the key and sum the value, to have:</p>
  
  <pre><code>[{'programming': 32},
   {'music': 10}]
  </code></pre>
  
  <p>sort them by the value (in this case, first programming and then music) and limit by 3 (if there is <code>programming 32</code>, <code>music 10</code>, <code>shopping 3</code> and <code>lifestyle 1</code>, only return <code>programming</code>, <code>shopping</code> and <code>lifestyle</code>.</p>
  
  <p>How to aggregate such data? Should I use mapreduce, aggregate or simply group? If so, how to tell group to group by the dictionary key?</p>
  
  <p>Thanks </p>