Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-3111

$cond incorrectly evaluates `undefined` in the projection document

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.0.0
    • Component/s: Shell

      Problem Description

      Compass inconsistently evaluates a Boolean expression containing undefined in $cond and returns inconsistent results. See Steps to Reproduce for syntax. Consistent evaluation would either produce different evaluation results for each object in the result set or would error out, whichever path is defined for this evaluation.

      Mongo Shell evaluates the same projection consistently and returns different results.

      Steps to Reproduce

      Insert these documents:

      db.cond.insertMany([{a: [1, 2, 3], b: "A"}, {b: "B"}]);

      Run this query in Mongo Shell:

      db.cond.find({}, {_id: 0, has_a: {$cond: [{$eq: ["$a", undefined]}, false, true]}});

      Run a query with the same projection in Compass. Compare both results (described below).

      Expected Results

      Mongo Shell returns these results:

      { "has_a" : true }
      { "has_a" : false } 

      Actual Results

      Compass returns these results:

      { has_a: true }
      { has_a: true } 

      Additional Notes

      Node.js driver behaves the same way as Compass, so it looks like it's really an issue with the Node.js driver.

      Changing $cond to this:

      db.cond.find({}, {_id: 0, has_a: {$cond: ["$a", true, false]}}); 

      , produces correct results in Compass and Node.js.

      Interestingly enough, Mongo Shell fails with an error when undefined is in the query, while Node.js driver and Compass evaluate the same just fine (e.g. {a: {$eq: undefined}}), so there's another bug in there somewhere.

       

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            cis74633@bell.net Andre M
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: