How to check if a field is not existing inside lookup
expression? The answers in similar questions are not helpful (e.g. {$eq : null}
or $exists:true
).
For example, I want to lookup inventory
only if disabled
is not existing.
db.orders.aggregate([
{
$lookup: {
from: "inventory",
let: {item: "$item"},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: ["$sku", "$$item" ]
},
{
$eq: [ "$disabled", null ]
}
]
}
}
},
],
as: "inv"
}
}
])
A playground sample is here
2
Answers
you could use:
before the look up, to filter out the documents that you do not want to look up
You can use
$exists
outside the$expr
:See how it works on the playground example