How to subtract in mongodb php

$getdataPipeline = array( array( '$match' => array( 'project_id' => array('$in' => $mysql_project_id) // Validating project ID ), '$match' => array('project_id' => $project_id) ), array( '$group' => array( '_id' => array('pro_id' => '$project_id', 'uid' => '$user_id'), "wh" => array('$subtract' => array(array('$sum' => '$toltal_timein_minutes'), array('$sum' => '$holding_durationin_minutes'))) )) ); 

Request run:

  $ValidProjectIdInMongo = $collection->aggregate($getdataPipeline); 

I get an error like

Disable the exception "MongoResultException" with the message "localhost: 27017: exception: unknown operator of the group" $ subtract ''

+1
source share
1 answer

$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.

+3
source

All Articles