I'm running an aggregate on a Collection and am using a lookup to another child Collection.
Each child may or may not have a field (foo) that can contain a String.
I'd like to project a Boolean for if at least 1 of these fields has a String for any of the child subdocuments.
So I can have something like
db.Parent.aggregate([
{
'$lookup': {
'from': 'Children',
'localField': '_id',
'foreignField': 'parentId',
'as': 'CHILDREN'
}
}, {
childHasFoo: {
"$CHILDREN" : {$elemMatch: {foo: {$ne: ''}}}
}
}
]
You can see my projection for childHasFoo . I'm thinking I can use $elemMatch or $anyElementTrue. Remember, foo may not exist on an/all of the children subdocuments either. So that needs to be taken into account.
Thanks!