When used in the $match stage, these two aggregation stages return different documents:
$expr: {
$eq: ["$cheese", undefined]
}
{
cheese: {$eq: undefined}
}
The field cheese
is not a property of any document in the collection.
In the second query, it returns all documents, which is expected behavior.
In the first query, it returns no documents.
What explains the difference?
CodePudding user response:
Using $expr
MongoDB will return always false
when compare with undefined
as is not a valid value to compare with.
But without $expr
mongo uses MongoDB query language (like a find
query) which will compare if the field exists.
Is that the reason because using the first query you do not receive anything (the comparsion is always false) and the second query return all documents (the field does not exist so the comparsion is always true).