mongoose-query-parser icon indicating copy to clipboard operation
mongoose-query-parser copied to clipboard

OR statement in URL query string

Open sudhish-ps opened this issue 4 years ago • 5 comments

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?.

sudhish-ps avatar Nov 11 '20 11:11 sudhish-ps

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 avatar Nov 13 '20 03:11 leodinas-hao

@leodinas-hao , Can we go with the operator | instead of ! which satisfies the technical meaning and purpose of its usage.

sudhish-ps avatar Nov 23 '20 11:11 sudhish-ps

@leodinas-hao, could you go through our proposal provided above.

sudhish-ps avatar Nov 27 '20 05:11 sudhish-ps

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.

leodinas-hao avatar Jul 08 '21 11:07 leodinas-hao

Any updates?

hatemragab avatar Dec 04 '22 15:12 hatemragab