Mongodb aggregates $ push with $ cond and $ each

I am trying to use $condconditionally $pushmultiple integers in an array of numbers during an aggregate $groupwithout any success. Here is my code:

Item.aggregate(
    [
      {
        $group: {
          _id: "$_id",
          numbers: {
            $push: {
              $cond: { 
                if: { $gt: [ "$price.percent", 70 ] }, 
                then: { $each: [10,25,50,70] },
                else: null,
              }
            }
          }
        }
      },
    ]
  )
  ...

Is Mongo DB just not configured for this right now, or am I looking at it all wrong?

+4
source share
1 answer

Please try without $eachas below

Item.aggregate(
    [{
        $group: {
          _id: "$_id",
          numbers: {
            $push: {
              $cond: { 
                if: { $gt: [ "$price.percent", 70 ] }, 
                then: [10,25,50,70] ,
                else: null,
              }
            }
          }
        }
      }]);
+3
source

Source: https://habr.com/ru/post/1629307/


All Articles