diff --git a/lib/model.js b/lib/model.js index 88c768f..84c3e1c 100644 --- a/lib/model.js +++ b/lib/model.js @@ -584,12 +584,15 @@ comparisonFunctions.$exists = function (value, exists) { } }; -arrayComparisonFunctions.$size = function (obj, value) { +// Specific to arrays +comparisonFunctions.$size = function (obj, value) { if (!util.isArray(obj)) { return false; } if (value % 1 !== 0) { throw "$size operator called without an integer"; } return (obj.length == value); }; +arrayComparisonFunctions.$size = true; + /** * Match any of the subqueries @@ -672,22 +675,20 @@ function match (obj, query) { /** * Match an object against a specific { key: value } part of a query + * if the treatObjAsValue flag is set, don't try to match every part separately, but the array as a whole */ -function matchQueryPart (obj, queryKey, queryValue) { +function matchQueryPart (obj, queryKey, queryValue, treatObjAsValue) { var objValue = getDotValue(obj, queryKey) , i, keys, firstChars, dollarFirstChars; // Check if the object value is an array - if (util.isArray(objValue)) { - // Check if it's a query operator array + if (util.isArray(objValue) && !treatObjAsValue) { + // Check if we are applying a comparison function to the array itself if (queryValue !== null && typeof queryValue === 'object' && !util.isRegExp(queryValue)) { keys = Object.keys(queryValue); - firstChars = _.map(keys, function (item) { return item[0]; }); - dollarFirstChars = _.filter(firstChars, function (c) { return c === '$'; }); - - if (keys.length == 1 && dollarFirstChars.length > 0 && arrayComparisonFunctions[keys[0]]) { - if (!arrayComparisonFunctions[keys[0]](objValue, queryValue[keys[0]])) { return false; } - return true; + + for (i = 0; i < keys.length; i += 1) { + if (arrayComparisonFunctions[keys[i]]) { return matchQueryPart(obj, queryKey, queryValue, true); } } }