[SERVER-20166] Aggregation's $eq expression doesn't match query semantics when a field is missing Created: 27/Aug/15  Updated: 26/Apr/19  Resolved: 04/Nov/15

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 3.1.7
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Charlie Swanson Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-13903 inconsistent treatment of null in $pr... Backlog
Backwards Compatibility: Major Change
Participants:

 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
      ]
    }
  }
}])



 Comments   
Comment by Ian Whalen (Inactive) [ 26/Apr/19 ]

Switching "Drivers Changes Needed" from "Maybe" to "Not Needed" since this was closed as something other than Fixed.

Generated at Thu Feb 08 03:53:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.