mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
minimongo: Implement ordinal indexing: {'foo.1.bar': 42}
This commit is contained in:
@@ -795,9 +795,12 @@ Tinytest.add("minimongo - selector_compiler", function (test) {
|
|||||||
match({$where: "_.isArray(this.a)"}, {a: []});
|
match({$where: "_.isArray(this.a)"}, {a: []});
|
||||||
nomatch({$where: "_.isArray(this.a)"}, {a: 1});
|
nomatch({$where: "_.isArray(this.a)"}, {a: 1});
|
||||||
|
|
||||||
|
match({"dogs.0.name": "Fido"}, {dogs: [{name: "Fido"}, {name: "Rex"}]});
|
||||||
|
match({"dogs.1.name": "Rex"}, {dogs: [{name: "Fido"}, {name: "Rex"}]});
|
||||||
|
nomatch({"dogs.1.name": "Fido"}, {dogs: [{name: "Fido"}, {name: "Rex"}]});
|
||||||
|
|
||||||
// XXX still needs tests:
|
// XXX still needs tests:
|
||||||
// - $elemMatch
|
// - $elemMatch
|
||||||
// - people.2.name
|
|
||||||
// - non-scalar arguments to $gt, $lt, etc
|
// - non-scalar arguments to $gt, $lt, etc
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -291,8 +291,6 @@ LocalCollection._selectorIsId = function (selector) {
|
|||||||
return (typeof selector === "string") || (typeof selector === "number");
|
return (typeof selector === "string") || (typeof selector === "number");
|
||||||
};
|
};
|
||||||
|
|
||||||
// XXX implement ordinal indexing: 'people.2.name'
|
|
||||||
|
|
||||||
// Given an arbitrary Mongo-style query selector, return an expression
|
// Given an arbitrary Mongo-style query selector, return an expression
|
||||||
// that evaluates to true if the document in 'doc' matches the
|
// that evaluates to true if the document in 'doc' matches the
|
||||||
// selector, else false.
|
// selector, else false.
|
||||||
@@ -396,19 +394,31 @@ LocalCollection._exprForKeypathPredicate = function (keypath, value, literals) {
|
|||||||
// drilling down through the dotted parts
|
// drilling down through the dotted parts
|
||||||
var ret = '';
|
var ret = '';
|
||||||
var innermost = true;
|
var innermost = true;
|
||||||
|
var lastPartWasNumber = false;
|
||||||
while (keyparts.length) {
|
while (keyparts.length) {
|
||||||
var part = keyparts.pop();
|
var part = keyparts.pop();
|
||||||
|
var thisPartIsNumber = false;
|
||||||
|
if (/^\d+/.test(part)) {
|
||||||
|
part = +part;
|
||||||
|
thisPartIsNumber = true;
|
||||||
|
}
|
||||||
var formal = keyparts.length ? "x" : "doc";
|
var formal = keyparts.length ? "x" : "doc";
|
||||||
if (innermost) {
|
if (innermost) {
|
||||||
ret = '(function(x){return ' + predcode + ';})(' + formal + '&&' + formal + '[' +
|
ret = '(function(x){return ' + predcode + ';})(' + formal + '&&' + formal + '[' +
|
||||||
JSON.stringify(part) + '])';
|
JSON.stringify(part) + '])';
|
||||||
innermost = false;
|
innermost = false;
|
||||||
|
} else if (lastPartWasNumber) {
|
||||||
|
// The last part was an array index, so if we find an array here we
|
||||||
|
// shouldn't search it!
|
||||||
|
ret = '(function(x){return ' + ret + ';})(' + formal + '&&' + formal + '[' +
|
||||||
|
JSON.stringify(part) + '])';
|
||||||
} else {
|
} else {
|
||||||
// for all but the innermost level of a dotted expression,
|
// If the runtime type is an array, search it, unless we're already at the
|
||||||
// if the runtime type is an array, search it
|
// innermost bit, or if the next part is a number (ie, an array index).
|
||||||
ret = 'f._matches(' + formal + '&&' + formal + '[' + JSON.stringify(part) +
|
ret = 'f._matches(' + formal + '&&' + formal + '[' + JSON.stringify(part) +
|
||||||
'], function(x){return ' + ret + ';})';
|
'], function(x){return ' + ret + ';})';
|
||||||
}
|
}
|
||||||
|
lastPartWasNumber = thisPartIsNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
Reference in New Issue
Block a user