As the doc says here : https://docs.atlas.mongodb.com/atlas-search/performance/#-match-aggregation-stage-usage
It’s not recommanded to use a $match in a $search query because it can drastically slow down query results.
In my case, I need to filter on a number field so I am using a compound search like this :
db.getCollection("movies").aggregate(
[
{
$search: {
"index":"year",
"compound": {
"must": [{
"text": {
"query": "war",
"path": "plot"
}
}],
"filter": [{
"range": {
"path": "year",
"gte": 1980,
"lte":1980
}
}]
}
}
},
{
$project: {"title":1,"year":1,"countries":1,"plot":1}
}
]
As you can see I want to filter my movies released only in 1980, so I’am using a range filter with gte=lte=1980.
It’s the only way I found to do this without using a $match stage (that ideed, decreases the perf), but I think this is not very elegant.
Is there another way ?
Thank you.