Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

312
Visualizações
Python function that takes a json object returns mongodb query object

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

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Hopefully I understood the question right

def splitToItems(query, items={}):
    q = {}
    if 'and' in query:
        l = []
        for item in query['and']:
           l.append(splitToItems(item))
        q["$and"] = l

    if 'or' in query:
        l = []
        for item in query['or']:
           l.append(splitToItems(item))
        q["$or"] = l

    if q:
        return q
    return query

print(splitToItems(query))

Does this solve your problem?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda