-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: 4.0.0
-
Component/s: Shell
-
Empty show more show less
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.