How to optimize $lookup when only one of two fields (notificationId or schedulerId) exists per document?

Yes, it will. Even if there’s no match, the lookup has to occur to establish that there’s no match.

Worth measuring and based on how large. If you’re looking up on the _id field, it will still be quite fast.

To improve performance, you can use $facet or $unionWith; both with a $match operation for the fields.

If there’s a group operation, then facet works well. But there’s a 100MB per stage limit and the standard document limit of 16 MB for the final result . So if you want full/all documents, then you will need unionWith.

Do you have some example data?