I have some condition in excel format like this:
AND(name = 'bob', age < 10) and it can be nested also.
I want to convert it into mongo format for querying data:
This should be translated into:
Excel condition: AND(name <> 'bob', age < 10)
equivalent mongo Condition: {$and: [{name: {$eq: 'bob'}},{age: {$lt: 10}}]}
This query can be nested also like and inside or and OR and or inside and. Can anyone suggest me some method for this.
Nesting Examples: It can be nesting of 'AND' and 'OR' multiple times: Eg:
1 - OR(AND(name <> 'bob', age < 10),AND(name <> 'john', age > 40), age = 40)
Equivalent Mongo Query: {$or: [{$and: [{name: {$ne: 'bob'}}, {age: {$lt: 10}}]}, {$and: [{name: {$ne: 'john'}}, {age: {$gt: 10}}]}, age: {$eq: 40}]}
2- AND(OR(name <> 'bob', age < 10),AND(name <> 'john', age > 40), age = 40)
Equivalent Mongo Query: {$and: [{$or: [{name: {$ne: 'bob'}}, {age: {$lt: 10}}]}, {$or: [{name: {$ne: 'john'}}, {age: {$gt: 40}}]}, age: {$eq: 40}]}
Thanks in advance.
-
Could you add an example of the nesting that you're talking about? That feels like it's the crux of your question & it's hard to answer without seeing itklhr– klhr2019年02月19日 15:15:14 +00:00Commented Feb 19, 2019 at 15:15
-
Hi @willis.. Please have a look. I have edited it..No one– No one2019年02月19日 18:44:13 +00:00Commented Feb 19, 2019 at 18:44
-
So, rephrasing the question a little bit -- you're trying to write a simple parser to transform a string excel condition into a mongo query?klhr– klhr2019年02月20日 04:15:20 +00:00Commented Feb 20, 2019 at 4:15
-
yes.. I want to convert themNo one– No one2019年02月20日 10:47:48 +00:00Commented Feb 20, 2019 at 10:47
-
stackoverflow.com/questions/9814528/… has details about how one would tackle a problem like this.klhr– klhr2019年02月20日 16:52:42 +00:00Commented Feb 20, 2019 at 16:52