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.

asked Feb 19, 2019 at 13:53
5
  • 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 Commented Feb 19, 2019 at 15:15
  • Hi @willis.. Please have a look. I have edited it.. Commented 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? Commented Feb 20, 2019 at 4:15
  • yes.. I want to convert them Commented Feb 20, 2019 at 10:47
  • stackoverflow.com/questions/9814528/… has details about how one would tackle a problem like this. Commented Feb 20, 2019 at 16:52

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.