I need to take a JSON (conditions to filter data), example data:
query = { "and": [
{
"field": "model",
"operator": ">",
"value": "2005"
},
{
"or": [
{
"field": "brand",
"operator": "=",
"value": "Mercedes"
},
{
"field": "brand",
"operator": "=",
"value": "BMW"
}
]
}
]
and write a function that transforms this JSON as a MongoDB query so I can directly filter the data while reading from MongoDb (I will use this data inside of apache beam ReadFromMongoDB function, as filter object)
so the output should be like:
{'$and': [{'field': 'model', 'operator': '>', 'value': '2005'}, {
'$or': [
{'field': 'brand', 'operator': '=', 'value': 'Mercedes'},
{'field': 'brand', 'operator': '=', 'value': 'BMW'}
]
}]}
( I have a function that transforms each single object into a MongoDB query so don't worry about that)
WHAT I TRIED
As I don't know how nested data will come, I used recursion in my function. But it becomes complicated and output is not as I expected.
def splitToItems(query, items={}):
if 'and' in query:
for item in query['and']:
if 'and' in item or 'or' in item:
splitToItems(item)
else:
if '$and' not in items:
items['$and'] = [item]
# print(items)
else:
items['$and'].append(item)
# print(items)
if 'or' in query:
for item in query['or']:
if 'and' in item or 'or' in item:
return splitToItems(item)
else:
if '$or' not in items:
items['$or'] = [item]
# print(items)
else:
items['$or'].append(item)
# print(items)
print(items)
-------
OUTPUT:
# '$or' should have been inside of the first array in this case
{
'$and': [{'field': 'model', 'operator': '>', 'value': '2005'}],
'$or': [{'field': 'brand', 'operator': '=', 'value': 'Mercedes'}, {'field': 'brand', 'operator': '=', 'value': 'BMW'}]
}
Any idea on how to improve this function or make this work? Many thanks in advance