dshm8998473 2015-02-05 09:12
浏览 59

PHP聚合 - 两个字段的总和

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)