Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8666915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:50:48+00:00 2026-06-12T17:50:48+00:00

I have a python dictionary like this {‘OR’: [{‘AND’: [{‘column’: ‘XXX’, ‘operator’: ‘=’, ‘value’:

  • 0

I have a python dictionary like this

{'OR': [{'AND': [{'column': 'XXX', 'operator': '=', 'value': u'M'}, {'column': 'XXX', 'operator': '=', 'value': 'N'}]}, {'column': 'YYY', 'operator': '>=', 'value': '3.0'}]}

Now, i want to convert it to something like

{'$or': [{'$and': [{'XXX': 'M'}, {'YYY': 'N'}]}, {'YYY': {u'$gte': 3.0}}]}

Which is clearly the equivalent pymongo statement I believe.

the code I have written so far is like this :

FILTMAP = {'>=': '$gte', '<=': '$lte', '>': '$gt', '<': '$lt', "!=":"$ne"}
CONJUNCTION_MAP = {"AND":"$and", "OR":"$or"}

def gen_mongo_filters_json(filter, supplied_key="")
    first_key = filters.keys()[0]
    if first_key == 'OR' or first_key == 'AND':

        if supplied_key == "":      

            return_dict[CONJUNCTION_MAP[first_key]] = []
        else:            
            temp_dict[CONJUNCTION_MAP[first_key]] = []
            #return_dict[supplied_key] = temp_dict
        for i in range (len(filters[first_key])):
            if supplied_key == "":               
                return_dict[CONJUNCTION_MAP[first_key]].append(gen_mongo_filters_json(filters[first_key][i], first_key))
            else:
                temp_dict[CONJUNCTION_MAP[first_key]].append(gen_mongo_filters_json(filters[first_key][i], first_key))
                return_dict[CONJUNCTION_MAP[supplied_key]].append(temp_dict)
    else:
        operator = filters['operator']
        if operator == "=":
            ret_dict = {filters['column']:filters['value']
            return ret_dict
        else:
            operator = FILTMAP[operator]
            ret_dict = {filters['column']:{operator:filters['value']}}
            return ret_dict
    return return_dict

The output it generates is :

 {u'$or': [{u'$and': [{u'Engine': u'MSN'}, {u'Engine': u'Google'}]}, {u'$and': [{u'Engine': u'MSN'}, {u'Engine': u'Google'}]}, {...}, {u'Imps': {u'$gte': 3.0}}]}

Which is near to the solution but not the exact one. It works fine for dictionaries like

{'AND': [{'column': 'XXX', 'operator': '=', 'value': 'M'}, {'column': 'XXX', 'operator': '=', 'value': 'N'}]}

OR

{'column': 'YYY', 'operator': '>', 'value': '1000'}

can you point to me a direction?

(the idea is to create a generic one. so, i would like to generate equivalent to any valid python dictionary into pymongo statement. the minimum is the last one)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:50:50+00:00Added an answer on June 12, 2026 at 5:50 pm

    Your example code does not run, but given that a dict

    {'OR': [{'AND': [{'column': 'XXX', 'operator': '=', 'value': 'M'}, {'column': 'YYY', 'operator': '=', 'value': 'N'}]}, {'column': 'YYY', 'operator': '>=', 'value': '3.0'}]}
    

    should be convertet to

    {'$or': [{'$and': [{'XXX': 'M'}, {'YYY': 'N'}]}, {'YYY': {'$gte': 3.0}}]}
    

    use something like this:

    FILTMAP = {'>=': '$gte', '<=': '$lte', '>': '$gt', '<': '$lt', "!=":"$ne"}
    CONJUNCTION_MAP = {"AND":"$and", "OR":"$or"}
    
    def convert_column(dic):
        if not dic['operator'] in FILTMAP:
            return {dic['column']: dic['value']}
        else:
            value = float(dic['value']) if dic['operator'] == "!=" else dic['value']
            return {dic['column']: {FILTMAP[dic['operator']]: value}}
    
    def convert(dic):
        for k,v in dic.items():
            if isinstance(v, list):
                if k in CONJUNCTION_MAP:
                    k = CONJUNCTION_MAP[k]
                return {k: [convert(i) for i in v]}
            else:
                return convert_column(dic)
    

    I don’t know if it’s important to convert '3.0' to 3.0. The line

    value = float(dic['value']) if dic['operator'] == "!=" else dic['value']
    

    is quite hackish, you want replace it with some appropriate logic to handle those cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary in python like this: x = {country:{city:population}:......} and I want
I have a python dictionary of type defaultdict(list) This dictionary is something like this:
I have data in a python dictionary that I'd like to store in a
I have a 2D dictionary in python indexed by two IPs. I want to
Say I have a string in Python 3.2 like this: '\n' When I print()
I have a python dict like this: my_dict = {'find': ['http://time.com', 'http://find.com'], 'time': ['http://any.com',
if I have a dictionary like this >>> d = {10: 3, 100: 2,
I have followed this How do I JSON serialize a Python dictionary? and this
I have a human dictionary file that looks like this in eng.dic (image that
I have some Python dictionaries like this: A = {id: {idnumber: condition},.... e.g. A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.