In the previous question, I wanted to get the counter of the received groups using operations with pipelines. As suggested, I used the following:
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'}}} )
Now, knowing the score, I would like to display the results on the page, so I only need a subset of data . My initial thought was using $slice in data in the $project pipeline, for example:
... {$project: {data : { $slice: [20,20] }, total: 1}
But it turns out that $slice not a valid operation for $project . I tried a workaround:
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} )
But as it turned out, I executed another $unwind pipeline. Is there a better solution to achieve what I'm trying to do?