This post consists of: 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 extension using aggregate to implement the Sum method
Here's Codeigniter
MongoDB
an extension that I added when I was using the extension
Is the MongoDB
Aggregate
method used Mysql
in the Sum
implementation
/* 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 extension using aggregate to implement the Sum method