Mongo $group divide two fields and show the result

Viewed 659

I have this aggregate that group values, but I need to create a new field called "ticket" and his value is the division between "totalServices" and "totalValue"

 Schedule.aggregate([{
      $match: {
        store: req.body.store,
        scheduleStart: {
          $lte: start,
          $gte: req.body.period
        },
        status: {
          $in: resultStatus
        }
      }
    },
    {
      $group: {
        _id: {
          name: "$employee.name",
          id: "$employee.id"
        },
        totalValue: {
          $sum: "$value"
        },
        totalServices: {
          $sum: 1
        },
        totalComission: {
          $sum: "$comissionValue"
        }
      }
    },
    {
      $sort: sort
    },
    {
      $skip: req.body.limit * req.body.page
    }

Result of this aggregate now:

{
  _id: {
       id:"5b644e88120834468cff7025",
       name: "Jonh" 
     }, 
    totalValue: 35,
    totalServices: 1, 
    totalComission: 25
}

Result that I need:

   {
      _id: {
           id:"5b644e88120834468cff7025",
           name: "Jonh" 
         }, 
        totalValue: 35,
        totalServices: 1, 
        totalComission: 25,
        ticket: 35
    }

I've tried to use $project after $group to do that division but i didn't have any success doing this.

0 Answers
Related