title: Array subset in aggregation framework pipeline
body: <p>In a previous <a href="https://stackoverflow.com/questions/13529323/obtaining-group-result-with-group-count">question</a> I wanted to obtain a count of the resulting groups using pipeline operations. As suggested, I used the following:</p>
  
  <pre><code>db.test.aggregate(
      {$unwind: '$tags'}, 
      {$group:{_id: '$tags', count:{$sum:1}}},
      {$project:{tmp:{tag:'$_id', count:'$count'}}}, 
      {$group:{_id:null, total:{$sum:1}, data:{$addToSet:'$tmp'}}}
  )
  </code></pre>
  
  <p>Now having known the count, I would like to display the results by page so I would only need a subset of <code>data</code>. My initial thought would be using <code>$slice</code> on <code>data</code> within a <code>$project</code> pipeline like:</p>
  
  <pre><code>...
  {$project: {data : { $slice: [20,20] }, total: 1}
  </code></pre>
  
  <p>But it appears that <code>$slice</code> is not a valid operation for <code>$project</code>. I tried a workaround by doing:</p>
  
  <pre><code>db.test.aggregate(
      {$unwind: '$tags'}, 
      {$group:{_id: '$tags', count:{$sum:1}}},
      {$project:{tmp:{tag:'$_id', count:'$count'}}}, 
      {$group:{_id:null, total:{$sum:1}, data:{$addToSet:'$tmp'}}},
      {$unwind: '$data'},
      {$skip: 20},
      {$limit: 20}
  )
  </code></pre>
  
  <p>But as it appears, I performed another <code>$unwind</code> pipeline. Is there a better solution to achieve what I am trying to do?</p>