davidgtonge / backbone_query

A lightweight query api for Backbone Collections
MIT License
378 stars 29 forks source link

Combined $and and $or are not working #9

Closed Rob--W closed 11 years ago

Rob--W commented 12 years ago

Basic logic is not functioning in your API, see these tests: http://jsfiddle.net/RZJcJ/

Col = new Backbone.QueryCollection [
    {diff:1, equ:'ok', same: 'ok'},
    {diff:2, equ:'ok', same: 'ok'}
]
# Should return all items, but returns nothing
Col.query
  $and:
    equ: 'ok'
    $or:
      same: 'ok'
# Should return nothing, but all items are returned:
Col.query
  equ: 'bogus'  # This item is clearly unavailable
  $or:
    same: 'ok'
davidgtonge commented 11 years ago

Looking into this now

davidgtonge commented 11 years ago

Hi, the problem is with the formation of your query: http://jsfiddle.net/RZJcJ/1/ I'm updating the api, to accept different types of queries, which should make your second query work. At the moment, if you are using $and/$or/$not/$nor then you can't have any query parameters outside of those, parent key names. For your first query, the $or is incorrectly nested.

Here is a working version:

res1 = Col.query
  $and:
    equ: 'ok'
  $or:
    same: 'ok'

res2 = Col.query
  $and:
    equ: 'bogus'  # This item is clearly unavailable
  $or:
    same: 'ok'
Rob--W commented 11 years ago

@davidgtonge Yout "working" queries are changing the structure of the query. My demo was a simplified case of a complex query.

The first query ought to demonstrate that the following -incorrectly- results in an empty list:

$and: alwaystrue
$or:
     something: alwaystrue
     another:   alwaystrue
davidgtonge commented 11 years ago

Ok, the issue is nested $or,$and,$not,$nor queries. I've not used these before, but they are part of the mongo spec - so I'm including your fix in the next version.