I need to do combined text search and filter by another value in the collection, while also returning the total count of matching records.
await Person.aggregate([
{
$search: {
index: 'people_text_index',
'text': {
'query': filter.query,
'path': {
wildcard: '*'
}
},
count: {
type: 'total'
}
}
},
{
$match: {
deleted: { $ne: true }
}
},
{
$count: 'count'
},
{
$addFields: {
'mongoMeta': '$$SEARCH_META'
}
},
{ $skip : offset },
{ $limit: limit }
]);
If I do this I get a result with form [ { count: 26 } ]
. If I want both the total count and the results, should I be running this twice? Once with the $count
and once without?