Mongo query using expr and let with missing value

hi, i want to optional lookup a collection to my original collection. for that i had to use lookup pipeline. but sometimes the value that join 2 collections together is not available. In this case i expect it just dont return any data. but mongo is scanning entire collection and comparing it with my let variable which is a null(not actually null. it has a type “missing”). as a result my query is super slow is there any way to fix this.please help mee

It sounds like you are using expressive $lookup with let rather than simple $lookup with localField/foreignField as this problem doesn’t show up with the latter.

The workaround is to use $ifNull expression to set the field you are testing for equality to explicit null value. We are already tracking this issue in this ticket but technically the engine is doing the “correct thing”, it’s just suboptimal in your case.