Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda