$sum is the battery that will be used with $group , so it must be used by the top-level operator. Therefore, your other operations should happen "inside" $sum :
$getdataPipeline = array( array( '$match' => array('project_id' => $project_id) ), array( '$group' => array( '_id' => array('pro_id' => '$project_id', 'uid' => '$user_id'), "wh" => array( '$sum' => array( '$subtract' => array( '$toltal_timein_minutes', '$holding_durationin_minutes' ) ) ) ) ) );
You can do it here because it is a basic subtraction, but more complex operations usually require a separate $project step after $group .
Also note that the yout $match pipeline script is incorrect and will actually be interpreted in the same way as I rewrote above. Perhaps you mean the $in condition for both possible values, which is a logical $or .
You also have what looks like input errors in field names.
source share