0

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.

No one
  • 1,130
  • 1
  • 11
  • 21
  • 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 it – klhr Feb 19 '19 at 15:15
  • Hi @willis.. Please have a look. I have edited it.. – No one Feb 19 '19 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 Feb 20 '19 at 04:15
  • yes.. I want to convert them – No one Feb 20 '19 at 10:47
  • https://stackoverflow.com/questions/9814528/recursive-descent-parser-implementation has details about how one would tackle a problem like this. – klhr Feb 20 '19 at 16:52

0 Answers0