Conditional $lookup

$facet is definitely not the way to go. However, if the field you are doing $lookup on is an empty array then it won’t match anything in the foreign collection (because array is used sort of as {$in:[]} which matches nothing.

So the solution to your problem may just be to fill in something like: {$set:{fruit_name:{$ifNull:["$fruit_name", [] ]}}} before the $lookup and the empty [] as localField won’t match anything, unlike null or missing field.

Asya

3 Likes