From a72c2b89a87dece2937c523d03ebbb6b70ea7dfc Mon Sep 17 00:00:00 2001 From: David Glasser Date: Sat, 28 Dec 2013 23:47:15 -0800 Subject: [PATCH] Move $exists to the new model Fixes two incompatibilities: - {$exists: false} did the wrong logic when there were multiple branches (we (poorly) special-cased $not and $nin but not this negative case) - No longer require the argument to $exists to be a boolean; {$exists: 0} and {$exists: 1} should work, eg --- packages/minimongo/minimongo_tests.js | 9 +++++++++ packages/minimongo/selector.js | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/minimongo/minimongo_tests.js b/packages/minimongo/minimongo_tests.js index 6ae30c5437..363d476beb 100644 --- a/packages/minimongo/minimongo_tests.js +++ b/packages/minimongo/minimongo_tests.js @@ -433,6 +433,15 @@ Tinytest.add("minimongo - selector_compiler", function (test) { nomatch({a: {$exists: false}}, {a: [1]}); match({a: {$exists: false}}, {b: [1]}); + match({a: {$exists: 1}}, {a: 5}); + match({a: {$exists: 0}}, {b: 5}); + + nomatch({'a.x':{$exists: false}}, {a: [{}, {x: 5}]}); + match({'a.x':{$exists: true}}, {a: [{}, {x: 5}]}); + match({'a.x':{$exists: true}}, {a: [{}, {x: 5}]}); + match({'a.x':{$exists: true}}, {a: {x: []}}); + match({'a.x':{$exists: true}}, {a: {x: null}}); + // $mod match({a: {$mod: [10, 1]}}, {a: 11}); nomatch({a: {$mod: [10, 1]}}, {a: 12}); diff --git a/packages/minimongo/selector.js b/packages/minimongo/selector.js index d2cde73201..a793c737c5 100644 --- a/packages/minimongo/selector.js +++ b/packages/minimongo/selector.js @@ -258,6 +258,12 @@ var VALUE_OPERATORS = { $nin: function (operand) { return invertBranchedSelector(convertElementSelectorToBranchedSelector( ELEMENT_OPERATORS.$in(operand))); + }, + $exists: function (operand) { + var exists = convertElementSelectorToBranchedSelector(function (value) { + return value !== undefined; + }); + return operand ? exists : invertBranchedSelector(exists); } }; @@ -1038,7 +1044,6 @@ var andCompiledDocumentSelectors = function (selectors) { // Remaining to update: -// - $exists // - $type // - $regex/$option // - $all