aslagle / reactive-table

A reactive table designed for Meteor
https://atmospherejs.com/aslagle/reactive-table
Other
328 stars 138 forks source link

$or and $and operators in custom Filters #471

Open AZWN opened 7 years ago

AZWN commented 7 years ago

The Mongo operators $or and $and do not work when set from a custom filter.

Example code:

var selectedValues = $('#multipleSelect').val(); // ["val1", "val2"]
template.filter.set({ $or: selectedValues });

Resulting stacktrace:

Exception in template helper: Error: Unrecognized operator: $or
    at http://localhost:3000/packages/minimongo.js?hash=68d94baf766fd5bcc86fc44a6758779727eb60f8:1535:13
    at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?hash=cde485f60699ff9aced3305f70189e39c665183c:157:22)
    at operatorBranchedMatcher (http://localhost:3000/packages/minimongo.js?hash=68d94baf766fd5bcc86fc44a6758779727eb60f8:1519:5)
    at compileValueSelector (http://localhost:3000/packages/minimongo.js?hash=68d94baf766fd5bcc86fc44a6758779727eb60f8:1431:12)
    at http://localhost:3000/packages/minimongo.js?hash=68d94baf766fd5bcc86fc44a6758779727eb60f8:1411:26
    at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?hash=cde485f60699ff9aced3305f70189e39c665183c:157:22)
    at compileDocumentSelector (http://localhost:3000/packages/minimongo.js?hash=68d94baf766fd5bcc86fc44a6758779727eb60f8:1398:5)
    at http://localhost:3000/packages/minimongo.js?hash=68d94baf766fd5bcc86fc44a6758779727eb60f8:1546:12
    at Array.map (<anonymous>)
    at Function._.map._.collect (http://localhost:3000/packages/underscore.js?hash=cde485f60699ff9aced3305f70189e39c665183c:167:56)

Without looking at the code I can imagine that the mongo query generated by the package looks like { myFilterField: { $or: selectedValues }, while you would expect $or: [ { myFilterField: 'val1' }, myFilterField: 'val2' }].

This might be related to #444

aslagle commented 7 years ago

You're right, $and and $or don't work inside filters. As a workaround you can create multiple filters and set the filterOperator argument to reactiveTable to $and or $or. It won't handle nested ands or ors, but I think it's enough for most use cases.

AZWN commented 7 years ago

In our case, we had defined custom filter using a multiple select($or), among other filters($and). That case is indeed not possible now. I solved it by joining the selected values with a |, and passing that using the $regex query.