Will $lookup still be slow if it is joining by _id?

Suppose that after $match stage, 10,000 documents will be passed into a $lookup stage. For each of those 10,000 documents, the $lookup will need to join based on its _id. The foreign field to join by will be indexed and almost unique - as it is made up of _id.

Will each $lookup for each of the 10,000 documents have O(1) due to the index?

So is it safe to assume that this pipeline will be fast and scalable?

this does not sound good for this lookup.

Can you explain? Does it need to be unique in order to scale?

$lookup (aggregation) — MongoDB Manual

though there are some limitations to using indexes in lookup, an index speeds up finding matching documents to O(1) if index keys are selected carefully to provide uniqueness.The _id field is one such key that should be unique alone.

O(1) means your document is unique and instantly picked up from the index. if your _id is not unique, you will have more than one reading for the same key leading to an O(n) scale in the worst case (imagine all documents have the same id).

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