davidgtonge / underscore-query

MongoDB like query api for JavaScript Arrays

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

$or operator on right hand side?

opened this issue · comments

It would be really helpful to apply the $or operator (or something like it) to just the right hand side of the constraint objects.

This is similar to how the $not operator can work on both the left and right sides. However, it's a bit opposite in that having $not operate on the right is standard MongoDB and the left is a useful nonstandard extension, here the $or is on the left in MongoDB and a useful extension is to have it work on the right.

  it "$or operator", ->
    a = create()

    # This is standard MongoDB syntax, and the test passes
    result = _.query a, {$or: [{likes: {$gt:1, $lte:2}}, {likes: {$gt:11, $lte:12}}]}
    assert.equal result.length, 2

    # This is not standard MongoDB syntax but I would like it to work
    result = _.query a, {likes:{ $or: [{$gt:1, $lte:2}, {$gt:11, $lte:12}]}}
    assert.equal result.length, 2

The underscore-query code is really elegant underneath. Would love to simply provide this as a pull request, but after spending a bit of time looking at the code, am seeking a bit of guidance the right place to start.

I don't think this should be supported