For anyone coming here, $unionWith
is the way to go.
To use the original question’s context, in your aggregation, initially match the records where fruit_name
is null, then union that with the records where fruit_name
is defined, and perform the lookup operation within the unionWith pipeline. Something like this:
db.child_fruit.aggregate([
{ $match: { fruit_name: null }},
{ $unionWith: {
coll: "child_fruit",
pipeline: [
{ $match: { fruit_name: { $ne: null }}},
{ $lookup: {
from: "fruit",
...
}}
]
},
...
]);