1. 程式人生 > >用php實現mongodb 分組排序

用php實現mongodb 分組排序

$keys = array('type'=>1); //依據type欄位分組
$initial = array("items" => array('CreateTime'=>0)); //帶到reduce中初始化
$reduce = "function (obj, prev) { 
    if(obj.CreateTime > prev.items.CreateTime){  //按照CreateTime來排序
        prev.items = obj;
    }
    	    
}";
$cand = array('condition'=>array('status'=>1)); //查詢條件
$collection->group($keys, $initial, $reduce, $cond);