Uploaded image for project: 'Compass '
  1. Compass
  2. COMPASS-9054

$eq / $ne not working for checking undefined (similar to $exists)

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • No version
    • Affects Version/s: None
    • Component/s: None
    • None
    • Environment:
      OS: macOS
      node.js / npm versions:
      Additional info:
    • Not Needed
    • Developer Tools

      Problem Statement/Rationale

      Since there is no "$exists" clause in aggregations, we're checking for undefined. However, $eq & $ne checks don't work with either undefined or null. When we change the operators to anything else, ex. $gt, $gte, it works.

      The following does not work

       

      {
        $group: {
          _id: 1, 
          HasTranscriptId: { 
            $sum: { 
              $cond: [
                { $ne: ["$Data.Metadata.TranscriptId", undefined] }, 0, 1
              ] 
            } 
          }  
        }
      }

       
      When you change the operator to something other than $eq / $ne, the query works.

      {
        _id: { $gt: ["$Data.Metadata.TranscriptId", undefined] },
        HasTranscriptId: {$count: { } }
      } 

       

      Please be sure to attach relevant logs with any sensitive data redacted.
      How to retrieve logs for: Compass; Shell

      Steps to Reproduce

      Try checking for the existence of a field in an aggregation stage, ex. $group or $project. You can try to compare with null or undefined, the results are the same.

      Expected Results

      Expecting this accumulator to work with $eq / $ne:

      {
        $group: {
          _id: 1, 
          HasTranscriptId: { 
            $sum: { 
              $cond: [
                { $ne: ["$Data.Metadata.TranscriptId", undefined] }, 0, 1
              ] 
            } 
          }  
        }
      }

      Actual Results

      $eq / $ne does not work.

      {
        $group: {
          _id: 1, 
          HasTranscriptId: { 
            $sum: { 
              $cond: [
                { $ne: ["$Data.Metadata.TranscriptId", undefined] }, 0, 1
              ] 
            } 
          }  
        }
      }

      Additional Notes

      Alternatively, it would be great if $exists is expanded to work in aggregation stages or a similar operator is added.

      Results:

      query:

      {
        _id: { $gt: ["$Data.Metadata.TranscriptId", undefined] },
        HasTranscriptId: {$count: { } }
      } 

      output: 

       

      [{_id: false, HasTranscriptId: 6996}, {_id: true, HasTranscriptId: 3525}] 

       

      query:

      {
        _id: { $eq: ["$Data.Metadata.TranscriptId", undefined] },
        HasTranscriptId: {$count: { } }
      } 

      output: 

      [{_id: false, HasTranscriptId: 10521}] 

        1. CleanShot 2025-02-25 at 16.06.50@2x.png
          121 kB
          Ahmad Husseini
        2. CleanShot 2025-02-25 at 16.06.36@2x.png
          119 kB
          Ahmad Husseini
        3. CleanShot 2025-02-25 at 15.56.28@2x.png
          88 kB
          Ahmad Husseini
        4. CleanShot 2025-02-25 at 15.56.17@2x.png
          87 kB
          Ahmad Husseini

            Assignee:
            paula.stachova@mongodb.com Paula Stachova
            Reporter:
            ahmad.husseini@moduleq.com Ahmad Husseini
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: