Mongodb的GROUP谁能用起来

浏览:2864 发布日期:2015/10/20 分类:求助交流 关键字: Mongodb
很多复杂的查询必须用聚合。球大神
Mongo.class.php
public function group($keys,$initial,$reduce,$options=array()){
if(isset($options['table']) && $this->_collectionName != $options['table']) {
$this->switchCollection($options['table'],'',false);
}

$cache = isset($options['cache'])?$options['cache']:false;
if($cache) {
$key = is_string($cache['key'])?$cache['key']:md5(serialize($options));
$value = S($key,'','',$cache['type']);
if(false !== $value) {
return $value;
}
}

$this->model = $options['model'];
N('db_query',1);
$query = $this->parseWhere($options['where']);

if(C('DB_SQL_LOG')) {
$this->queryStr = $this->_dbName.'.'.$this->_collectionName.'.group({key:'.json_encode($keys).',cond:'.
json_encode($options['condition']) . ',reduce:' .
json_encode($reduce).',initial:'.
json_encode($initial).'})';
}
try{
// 记录开始执行时间
G('queryStartTime');

$option = array('condition'=>$options['condition'], 'finalize'=>$options['finalize'], 'maxTimeMS'=>$options['maxTimeMS']);
$group = $this->_collection->group($keys,$initial,$reduce,$options);
$this->debug();

if($cache && $group['ok'])
S($key,$group,$cache['expire'],$cache['type']);

return $group;
} catch (\MongoCursorException $e) {
throw_exception($e->getMessage());
}
}

MongoModel.class.php
public function group($key, $init, $reduce, $option=array())
{
$option = $this->_parseOptions($option);

//合并查询条件
if(isset($option['where']))
{
if(!isset($option['condition']))
$option['condition'] = array();
$option['condition'] = array_merge($option['condition'], $option['where']);
}

return $this->db->group($key, $init, $reduce, $option);
}
最佳答案
评论( 相关
后面还有条评论,点击查看>>