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

133
Visualizações
Mongodb $cond inside $cond

I have to apply group by operation on Campaign_Name and then sum aggregator on a field which will be decided based on some condition. I am facing some syntax error in the below code sample:

   $group : {
              _id: "$Campaign_Name",
              Basecount: {
                $sum: {
                  $cond: [
                    { "Recharge_Flag_Overall": { $eq: "NA"} },
                    $cond: [
                      { "Plus_Orn" : { $eq: 0} },
                      "$Winback_Orn",
                      "$Plus_Orn"
                    ],
                    "$Basecount"
                  ]
                }
              }
            }

Now, the condition for the sum aggregator is that if Recharge_Flag_Overall value is "NA", then I need to check the value of Plus_Orn and, if it is 1, then I have to take the sum of Plus_Orn, else the sum of the field named Winback_Orn. Otherwise, if the Recharge_Flag_Overall value is not "NA", then simply, I have to take the sum of a field named Basecount

Note: Plus_Orn and Winback_Orn have only 0 or 1 as their values

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

0

You are almost there, just have to correct the syntax of $eq operator,

  {
    $group: {
      _id: "$Campaign_Name",
      Basecount: {
        $sum: {
          $cond: [
            { $eq: ["$Recharge_Flag_Overall", "NA"] },
            {
              $cond: [
                { $eq: ["$Plus_Orn", 0] },
                "$Winback_Orn",
                "$Plus_Orn"
              ]
            },
            "$Basecount"
          ]
        }
      }
    }
  }
about 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