I have a query as below:
$pipeline = array(
array(
'$match' => $query,
),
array(
'$group' => array(
'_id' => '$_id',
's_count' => array('$sum' => array('$add' => array('value1' , 'value2' ))),
)
),
array(
'$sort' => array(
'id' => -1
)
)
);
$result = $this->db->aggregate( 'table',$pipeline );
In the above query, value 1 and value 2 are declared as strings in my table, but its values are numbers.
Is there any way to parse my fields as integer in group sum stage, as it is throwing error "Only numeric values allowed for $sum aggregation"
EDITED
My requirement is like: there are two fields value1
and value2
in my table.
I need to get all the records with sum of those two fields in an order ie max to min.
Because in my view page, I have to paginate and display. So at the time of pagination it is returning results in an order max to min within the 10 records( I have given sort condition after query processing).
I need to display 10 records first out of which it should be sorted in such a way that first record's sum of those two fields should be maximum in my table.. in that manner I have to display.