$facets compared to unionWith

Often questions like that are better answered by yourself using your own dataset since there is usually not a good answer for all cases.

And this is something very easy to test. Writing an $unionWith should take only a few hours.

But in my opinion, I think the union with would be faster because you do not do anything before the $facet.

But in my opinion, you should not trust anyone who does not back his claims with data.

But in my opinion too, $facet might be faster depending of your indexes. Because without proper index, $facet might do a single collection scan and unionWith will do to. With proper index you might end up with 2 index scan which are probably faster than 1 collection scan.

No clear answer, sorry.

If you do test please share the result.

1 Like