diff --git a/packages/minimongo/minimongo_tests.js b/packages/minimongo/minimongo_tests.js index 94ea0146e2..d939ac541c 100644 --- a/packages/minimongo/minimongo_tests.js +++ b/packages/minimongo/minimongo_tests.js @@ -908,7 +908,9 @@ Tinytest.add("minimongo - selector_compiler", function (test) { // $where match({$where: "this.a === 1"}, {a: 1}); + match({$where: "obj.a === 1"}, {a: 1}); nomatch({$where: "this.a !== 1"}, {a: 1}); + nomatch({$where: "obj.a !== 1"}, {a: 1}); nomatch({$where: "this.a === 1", a: 2}, {a: 1}); match({$where: "this.a === 1", b: 2}, {a: 1, b: 2}); match({$where: "this.a === 1 && this.b === 2"}, {a: 1, b: 2}); diff --git a/packages/minimongo/selector.js b/packages/minimongo/selector.js index 81632754ad..42798fecde 100644 --- a/packages/minimongo/selector.js +++ b/packages/minimongo/selector.js @@ -162,10 +162,14 @@ var LOGICAL_OPERATORS = { "$where": function(selectorValue) { if (!(selectorValue instanceof Function)) { - selectorValue = Function("return " + selectorValue); + // XXX MongoDB seems to have more complex logic to decide where or or not + // to add "return"; not sure exactly what it is. + selectorValue = Function("obj", "return " + selectorValue); } return function (doc) { - return selectorValue.call(doc); + // We make the document available as both `this` and `obj`. + // XXX not sure what we should do if this throws + return selectorValue.call(doc, doc); }; },