Here I am, a few years later. The original answer is not what I’m looking for. I don’t want to filter out results. I want the entire result set, but only do lookups on certain records.
For example, collection fruit and child_fruit. If I do db.child_fruit.aggregate(pipeline), I only want child_fruit records to perform $lookup against fruit collection if child_fruit HAS a value for a field “fruit_name”. For example:
##fruit collection
db.fruit.insert({name: ‘Orange’});
##child_fruit collection
db.child_fruit.insert({name: ‘Something’, fruit_name: ‘Orange’});
db.child_fruit.insert({name: ‘Another’});
db.child_fruit.aggregate([
{$lookup: {
from: ‘fruit’,
…
}}
]);
The problem is, given that I still want the “Another” record back in my final result set, I don’t believe there is a way to “ignore” the $lookup for child_fruit records without “fruit_name” field…
In this small example it’s irrelevant, but when I’m querying millions of records, many of which do not have a fruit_name field, it makes a huge difference. I want to just “bypass” the lookup operation in this case for these records. Maybe @Asya_Kamsky has an idea, she is a genius.