MongoDB aggregate by field exists
$ne: [ { $type : "$var_to_check"}, 'missing'] }
or
$ne : [$var_to_check, undefined]
# match stage eg:
{
$match: {
$expr: {
$ne: [{
$type: "$var_to_check"
},
'missing'
]
}
}
}
# or
{
$match: {
$expr: {
$ne: [$var_to_check, undefined]
]
}
}
}