標籤:
本篇文章由:http://xinpure.com/2015/01/07/codeigniter-mongodb%e6%89%a9%e5%b1%95%e4%b9%8b%e4%bd%bf%e7%94%a8aggregate%e6%96%b9%e6%b3%95%e5%ae%9e%e7%8e%b0mysql%e7%9a%84sum%e6%96%b9%e6%b3%95/
Codeigniter MongoDB擴充之使用Aggregate實現Sum方法
以下就是我在使用 Codeigniter 的 MongoDB 擴充時,添加的一個擴充
就是使用 MongoDB 的 Aggregate 實現 Mysql 中的 Sum 方法
/* Controller.php */$option = array ( array ( ‘$match‘ => array ( ‘match_1‘ => ‘value_1‘, ‘match_2‘ => ‘value_2‘ ) ), array ( ‘$group‘ => array ( ‘_id‘ => null, ‘sum_1‘ => [‘$sum‘ => ‘$amount_1‘], ‘sum_2‘ => [‘$sum‘ => ‘$amount_2‘], ‘sum_3‘ => [‘$sum‘ => ‘$amount_3‘] ) ));$result = $this -> mongo_db -> aggregate(‘collection‘, $option);/* Mongo_db.php */public function aggregate ($collection, $option = array()){ try { return $this -> db -> {$collection} -> aggregate($option); } catch (Exception $e) { show_error("Unable to aggregate Mongo Databases: {$e->getMessage()}", 500); }}
Codeigniter MongoDB擴充之使用Aggregate實現Sum方法