Lookup VS UnionWith - which has better performance?

When Joining TWO Collections (large collections 3M+) in an aggregation , which has better performance $Loookup or $UnionWith ?

I would not know how to do with one what the other is meant for.

I do not think there is a definitive answer to such a general question. You would need to share the $lookup and $unionWith code that you think will provide you the same result.

I wrote that I would not know how to do with one what the other is meant for, it is true but I could imagine that one could try to replace $lookup by downloading the 2 collections using $unionWith and the perform the work that $lookup does in their own code. I think that in this case $unionWith would be an order of magnitude slower. You code will not be able to leverage the power of any indexes when establishing the relationship that you would do in your $lookup.

But my intuitions have wrong a couple of times in the past in relation to performances so you should definitively test with you real data. You could then share your findings here an enlighten us.

Dear Steevej, thank you for your response , I think your intuition is insightful. I agree with your thoughts on the topic, especially when dealing with large (M+) collections, lookup would be much better than joining. I would add that Lookup based on INDEXED foreignField would be optimal.

Thank you

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.