Details
-
Improvement
-
Resolution: Duplicate
-
Major - P3
-
None
-
3.1.7
-
None
-
Major Change
Description
Documents without the field 'a' will match the query predicate {a: null}, but in the aggregation pipeline, documents without the field 'a' will not match the predicate {$eq: ['$a', null]}.
For example, suppose you had the following documents in the collection coll:
{_id: 0}
|
{_id: 1, a: null} |
Then a query with the predicate {a: null} will return both documents. However, the pipeline:
db.coll.aggregate([{$project: {matches: {$eq: ['$a', null]}}}]) |
produces the following documents:
{_id: 0, matches: false} |
{_id: 1, matches: true} |
This is inconsistent, and sometimes not the desired behavior. The aggregation $eq operator should treat nulls the same way the query $eq operator does. Or, there should be an $isNull operator, that behaves like the query predicate {$eq: ['field', null]} operator.
There is a workaround; the following pipeline would treat null and missing as equivalent:
db.coll.aggregate([{
|
$project: {
|
matches: {
|
$eq: [
|
{$ifNull: ['$a', null]}, |
null |
]
|
}
|
}
|
}])
|
Attachments
Issue Links
- duplicates
-
SERVER-13903 inconsistent treatment of null in $project comparisons vs other stages
-
- Backlog
-