title: limit and sort each group by in mongoDB using aggregation
body: <p>How can I sort and limit each group by in mongoDB.</p>
  
  <p>Consider below data:</p>
  
  <pre><code>Country:USA,name:xyz,rating:10,id:x
  Country:USA,name:xyz,rating:10,id:y
  Country:USA,name:xyz,rating:10,id:z
  Country:USA,name:abc,rating:5,id:x
  Country:India,name:xyz,rating:5,id:x
  Country:India,name:xyz,rating:5,id:y
  Country:India,name:abc,rating:10,id:z
  Country:India,name:abc,rating:10,id:x
  </code></pre>
  
  <p>Now say I will group by country and sort by rating and limit the data of each group by 2.</p>
  
  <p>so answer would be:</p>
  
  <pre><code>Country:USA
  name:xyz,rating:10,id:x
  name:xyz,rating:10,id:y
  Country:India
  name:abc,rating:10,id:x
  name:abc,rating:10,id:z
  </code></pre>
  
  <p>I want to accomplish this using aggregate framework only.</p>
  
  <p>I tried including sort in aggregate for rating but simply query turns no results after processing. </p>