I am unable to get both the number of total hits and data in the documents in one aggregation query. My aggregation pipeline looks like this:
{
'$match': {
'some_text': 'Stackoverflow',
'some_status': 1
}
},
{
'$count': 'hits'
}
All I get after the second stage is hits = N. Just 1 document giving the total number of documents matched. I'd like the pipeline to return other details as well, but according to the mongo docs "$count" stage returns a new document to the next stage of the pipeline. How can I use hits value at a later stage of the pipeline and get all document data also.
Desired result:
{
"hits": 20,
"data": [
{
"name": "Name1",
"age": 10
},
{
"name": "Name2",
"age": 23
}
]
}
Thanks.