Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-20166

Aggregation's $eq expression doesn't match query semantics when a field is missing

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.1.7
    • Component/s: Aggregation Framework
    • None
    • Major Change

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

            Assignee:
            Unassigned Unassigned
            Reporter:
            charlie.swanson@mongodb.com Charlie Swanson
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: