Skip to content

Commit

Permalink
Support for $all
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jan 25, 2016
1 parent 34bb2ca commit 43bed5c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Any query parameters other then _fields_, _omit_, _sort_, _offset_, and _limit_
* Regex patterns. For example, `name=/^john/i` yields `{id: /^john/i}`.
* Parameters without a value check that the field is present. For example, `foo&bar=10` yields `{foo: {$exists: true}, bar: 10}`.
* Parameters prefixed with a _not_ (!) and without a value check that the field is not present. For example, `!foo&bar=10` yields `{foo: {$exists: false}, bar: 10}`.
* Supports some of the named comparision operators ($type, $size). For example, `foo:type=string`, yeilds `{ foo: {$type: 'string} }`.
* Supports some of the named comparision operators ($type, $size and $all). For example, `foo:type=string`, yeilds `{ foo: {$type: 'string} }`.
### A note on embedded documents
Comparisons on embedded documents should use mongo's [dot notation](http://docs.mongodb.org/manual/reference/glossary/#term-dot-notation) instead of express's 'extended' [query parser](https://www.npmjs.com/package/qs) (Use `foo.bar=value` instead of `foo[bar]=value`).

Expand All @@ -146,3 +146,7 @@ npm test

## Todo
* Add support for forced string comparison; value in quotes (`field='10'`) would force a string compare. Should allow for string with embedded comma (`field='a,b'`).
* Geospatial search
* $text searches
* $mod comparision
* Bitwise comparisions
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ function comparisonToMongo(key, value) {
}
} else if (op[0] == ':' && op[op.length - 1] == '=') {
op = '$' + op.substr(1, op.length - 2)
var array = []
parts[3].split(',').forEach(function(value) {
array.push(typedValue(value))
})
value = { }
value[op] = typedValue(parts[3])
value[op] = array.length == 1 ? array[0] : array
} else {
value = typedValue(parts[3])
if (op == '>') value = {'$gt': value}
Expand Down
5 changes: 5 additions & 0 deletions test/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ describe("query-to-mongo(query) =>", function () {
assert.ok(results.criteria)
assert.deepEqual(results.criteria, {array: {$size: 2} })
})
it("should create $all criteria", function () {
var results = q2m("array:all=50,60")
assert.ok(results.criteria)
assert.deepEqual(results.criteria, {array: {$all: [50, 60] } })
})
})

describe(".options", function () {
Expand Down

0 comments on commit 43bed5c

Please sign in to comment.