diff --git a/query.js b/query.js index 20625fd..07f14c1 100644 --- a/query.js +++ b/query.js @@ -314,7 +314,7 @@ else if (constraint instanceof RegExp) return this.$regex(value, constraint) else { for (var key in constraint) { - if (!this[key]) return this.$eq(value, constraint, row, getter) + if (!this[key]) return this._satisfies(value[key], constraint[key], row, getter) else if (!this[key](value, constraint[key], row, getter)) return false; } return true; diff --git a/test/elemMatch.js b/test/elemMatch.js index 43983a4..8b2e3f9 100644 --- a/test/elemMatch.js +++ b/test/elemMatch.js @@ -151,6 +151,35 @@ describe("Underscore Query Tests", function () { } }); assert.equal(result.length, 1); - }) + var rows = [ + {title: "Home", comments:[ + {text:"I like this post"}, + {text:"I love this post"}, + {text:"I hate this post"} + ]}, + {title: "About", comments:[ + {text:"I like this page"}, + {text:"I love this page"}, + {text:"I really like this page"} + ]} + ]; + result = _.query(rows, { + comments: { + $elemMatch: { + text: "I really like this page" + } + } + }); + assert.equal(result.length, 1); + + result = _.query(rows, { + comments: { + $elemMatch: { + text: /really/i + } + } + }); + assert.equal(result.length, 1); + }) })