Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!