In our project we use the aggregation pipeline and $facet operator extensively.
When we use the explain: true option, we look at the query plan. But the output related to $facet is really limited, and does not mention anything about indexes etc.
How do you effectively analyze aggregation pipelines with the $match step?
Hi @Alex_Bjorlig thanks for your interest and glad to see you back in the forum. While I cannot say much just yet, we will be releasing something that I think your team will be able to use that leverages search indexes and facets in Lucene very soon. Stay tuned.
Can you provide an example of your aggregation pipeline and confirm your version of MongoDB server? The actual explain output would also be helpful if you have a specific query about outcomes.
The $facet stage, and its sub-pipelines, cannot make use of indexes, even if its sub-pipelines use $match or if $facet is the first stage in the pipeline. The $facet stage will always perform a COLLSCAN during execution.
Thanks for awesome answers - and now it makes more sense we get a bunch of warnings about collection scans, because we use facets for every operation. We implement cursor based pagination, and in a facet operation we:
Lkp the actuel rows to return
The total count
The paginated count
Optional facet results.
Are there any feature requests or issues tracking the fact the facet sub-pipelines can’t use an index?
We are already thinking about how to rewrite the resolvers - because this is not something we do in atlas-search, but “just” $facets in the aggregation pipeline.