Support "obj" name in $where

This commit is contained in:
David Glasser
2013-12-26 13:08:19 -08:00
parent 373413b550
commit 2263cd49b4
2 changed files with 8 additions and 2 deletions

View File

@@ -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});

View File

@@ -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);
};
},