I have to implement search using search indexes in MongoDB Atlas as well as normal browse feature. This includes filtering, match, sort, skip, limit (pagination). I have made an aggregation pipeline to achieve all this. First I push the search query to my pipeline, then match, then sort, then skip and finally the limit query.
Here's how it goes:
query = [];
query.push({
$search: {
index: 'default'
text: {
query: searchQuery
path: { }
}
}
});
query.push({
$sort: sort,
});
query.push({
$match: {
type: match
},
query.push({
$skip: skip
});
query.push({
$limit: perPage
});
let documents = await collection.aggregate(query);
The results I get so far are correct. However, for pagination, I also want to get the total count of documents. The count must take the "match" and "searchQuery" (if any) take into account.
I have tried $facet
but it gives error $_internalSearchMongotRemote is not allowed to be used within a $facet stage