mongoose-query-parser
mongoose-query-parser copied to clipboard
OR statement in URL query string
Need to support OR statement in REST API, and to build right query string for that. In some business case we have to filter the document with OR condition like below for e.g.;
From employee Filter position == 'Manager' OR age >= 50
How to generate such queries with the parser and how to define such condition in query string ?. Currently multiple query parameters specified in the query string is generating with AND condition only, but definitely there is need of OR also. However we can specify this condition as filter like this { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } but need is to simplify this in standard query format. Some thing like ?position=Manager&!age>=50 (where ! internally converts to OR).
Any option available?.
Hi @sudhish-ps , thanks for your comments. I had the similar thought as you mentioned the current logic doesn't handle OR
condition and actually it doesn't handle AND
as well. The real issue is it doesn't handle grouping. What you proposed using !&
still won't resolve the issue of no grouping. Also, it conflicts with not exists "!" filter operator.
If you have another idea on this one, please don't hesitate to share and I'm more than happy to implement a proper change to improve this tool.
@leodinas-hao , Can we go with the operator | instead of ! which satisfies the technical meaning and purpose of its usage.
@leodinas-hao, could you go through our proposal provided above.
Hi @sudhish-ps, operator | won't solve the problem as the OR
condition needs to in a group. Could you please have a look below example of how to do $or
filter by passing json in query?
https://github.com/leodinas-hao/mongoose-query-parser/blob/e29d68da17fe9b3c250fead3544ec29390fec52a/src/test.spec.ts#L131
Or please feel free to provide a PR for your solution?
Thanks.
Any updates?