🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# MongoYii # ## 文档 官方文档:http://sammaye.github.io/MongoYii/ github:https://github.com/Sammaye/MongoYii ## 安装 *D:\wamp\www\xue.test\xuetang\protected\config\common.php* ``` define("MONGO_HOST","10.8.8.14"); define("MONGO_PORT","27017"); define("MONGO_DB","weike"); define("MONGO_USER","mch"); define("MONGO_PWD","mch"); ... 'components'=>array( 'mongodb' => array( 'class' => 'EMongoClient', 'server' => 'mongodb://'.MONGO_HOST.':'.MONGO_PORT, 'db' => MONGO_DB, 'options' => array( 'connect' => true , //'username' => MONGO_USER, // 'password' => MONGO_PWD , ) ), ), ... 'import'=>array( ... 'application.extensions.MongoYii.*', 'application.extensions.MongoYii.validators.*', 'application.extensions.MongoYii.behaviors.*', 'application.extensions.MongoYii.util.*' , 'application.models.mongo.*', ... ), ``` ## 统计代码示例 聚合管道文档参考:http://www.mongoing.com/docs/aggregation.html *D:\wamp\www\xue.test\xuetang\protected\service\service2\V2StatisticsService.php* ``` /** * 统计每个用户类型登录数据 * @param array $args * @return array * @author wuzhc 2018-08-06 */ public function statLoginEachUserType($args) { /** @var EMongoClient $mongodb */ $mongodb = Yii::app()->mongodb; $collection = $mongodb->selectCollection(WK::XT_LOGIN_LOG); $res = $collection->aggregate(array( array( '$match' => array( 'date' => array( '$gte' => $args['begin'], '$lte' => $args['end'] ) ) ), array( '$group' => array( '_id' => '$userType', 'total' => array( '$sum' => 1 ) ) ), array( '$project' => array( '_id' => 0, 'userType' => '$_id', 'total' => '$total' ) ) )); return isset($res['result']) ? $res['result'] : array(); } /** * 统计每个地区登录数据 * @param array $args * @return array * @author wuzhc 2018-08-06 */ public function statLoginEachArea($args) { if ($args['areaID']) { /** @var Area $area */ $area = Area::model()->findByPk($args['areaID']); $level = $area->fdLevel; } else { $level = 0; } /** @var EMongoClient $mongodb */ $mongodb = Yii::app()->mongodb; $collection = $mongodb->selectCollection(WK::XT_LOGIN_LOG); $match['date'] = array('$gte' => $args['begin'], '$lte' => $args['end']); if ($level == 1) { $match['provinceID'] = (int)$args['areaID']; } elseif ($level == 2) { $match['cityID'] = (int)$args['areaID']; } $match['userType'] = array(WK::TEACHER_TYPE_ID, WK::STUDENT_TYPE_ID, WK::PARENT_TYPE_ID); $res = $collection->aggregate(array( array( '$match' => $match ), array( '$group' => array( '_id' => array( 'areaID' => $level == 0 ? '$provinceID' : ($level == 1 ? '$cityID' : '$regionID'), 'userType' => '$userType' ), 'total' => array( '$sum' => 1 ) ) ), array( '$project' => array( '_id' => 0, 'total' => '$total', 'areaID' => '$_id.areaID', 'userType' => '$_id.userType', ) ) )); return isset($res['result']) ? $res['result'] : array(); } /** * 统计每天登录数据 * @param array $args * @return array * @author wuzhc 2018-08-06 */ public function statLoginEachDay($args) { /** @var EMongoClient $mongodb */ $mongodb = Yii::app()->mongodb; $collection = $mongodb->selectCollection(WK::XT_LOGIN_LOG); $res = $collection->aggregate(array( array( '$match' => array( 'date' => array( '$gte' => $args['begin'], '$lte' => $args['end'] ) ) ), array( '$group' => array( '_id' => array( 'day' => '$day', 'userType' => '$userType' ), 'total' => array( '$sum' => 1 ) ) ), array( '$project' => array( '_id' => 0, 'userType' => '$_id.userType', 'day' => '$_id.day', 'total' => '$total' ) ) )); $data = array(); $records = isset($res['result']) ? $res['result'] : array(); foreach ($records as $r) { $day = $r['day']; if (!$day) { continue; } if (!isset($data[$day])) { $data[$day]['total'] = 0; $data[$day]['day'] = $day; $data[$day]['data'] = array(); } $data[$day]['total'] += $r['total']; $data[$day]['data'][] = array( 'total' => $r['total'], 'userType' => $r['userType'], ); } return $data; } /** * 统计每个用户类型注册数据 * @return array * @author wuzhc 2018-08-06 */ public function statRegisterEachUserType($args) { /** @var EMongoClient $mongodb */ $mongodb = Yii::app()->mongodb; $collection = $mongodb->selectCollection(WK::XT_REGISTER_LOG); $res = $collection->aggregate(array( array( '$match' => array( 'date' => array( '$gte' => $args['begin'], '$lte' => $args['end'] ) ) ), array( '$group' => array( '_id' => '$userType', 'total' => array( '$sum' => 1 ) ) ), array( '$project' => array( '_id' => 0, 'userType' => '$_id', 'total' => '$total' ) ) )); return isset($res['result']) ? $res['result'] : array(); } /** * 统计每个地区注册数据 * @return array * @author wuzhc 2018-08-06 */ public function statRegisterEachArea($args) { if ($args['areaID']) { /** @var Area $area */ $area = Area::model()->findByPk($args['areaID']); $level = $area->fdLevel; } else { $level = 0; } /** @var EMongoClient $mongodb */ $mongodb = Yii::app()->mongodb; $collection = $mongodb->selectCollection(WK::XT_REGISTER_LOG); $match['date'] = array('$gte' => $args['begin'], '$lte' => $args['end']); if ($level == 1) { $match['provinceID'] = (int)$args['areaID']; } elseif ($level == 2) { $match['cityID'] = (int)$args['areaID']; } $res = $collection->aggregate(array( array( '$match' => $match ), array( '$group' => array( '_id' => array( 'areaID' => $level == 0 ? '$provinceID' : ($level == 1 ? '$cityID' : '$regionID'), 'userType' => '$userType' ), 'total' => array( '$sum' => 1 ) ) ), array( '$project' => array( '_id' => 0, 'total' => '$total', 'areaID' => '$_id.areaID', 'userType' => '$_id.userType', ) ) )); return isset($res['result']) ? $res['result'] : array(); } /** * 统计每天注册数据 * @return array * @author wuzhc 2018-08-06 */ public function statRegisterEachDay($args) { /** @var EMongoClient $mongodb */ $mongodb = Yii::app()->mongodb; $collection = $mongodb->selectCollection(WK::XT_REGISTER_LOG); $res = $collection->aggregate(array( array( '$match' => array( 'date' => array( '$gte' => $args['begin'], '$lte' => $args['end'] ) ) ), array( '$group' => array( '_id' => array( 'day' => '$day', 'userType' => '$userType' ), 'total' => array( '$sum' => 1 ) ) ), array( '$project' => array( '_id' => 0, 'day' => '$_id.day', 'total' => '$total', 'userType' => '$_id.userType' ) ) )); $data = array(); $records = isset($res['result']) ? $res['result'] : array(); foreach ($records as $r) { $day = $r['day']; if (!$day) { continue; } if (!isset($data[$day])) { $data[$day]['total'] = 0; $data[$day]['day'] = $day; $data[$day]['data'] = array(); } $data[$day]['total'] += $r['total']; $data[$day]['data'][] = array( 'total' => $r['total'], 'userType' => $r['userType'], ); } return $data; } /** * 用户答卷记录统计 * @param $args * @return array */ public function getUsersExamRecordStat($args) { /** @var EMongoClient $mongo */ $mongo = Yii::app()->mongodb; $collection = $mongo->selectCollection(WK::XT_EXAM_RECORD_COLLECTION); // 参数 if (!is_array($args['uid'])) { $args['uid'] = (array)$args['uid']; } if ($args['uid']) { array_walk($args['uid'], function(&$uid){$uid = (int)$uid;}); } $args['begin'] = $args['begin'] ? new MongoDate($args['begin']/1000+8*60*60) : new MongoDate(strtotime('-1 year')); $args['end'] = $args['end'] ? new MongoDate($args['end']/1000+8*60*60) : new MongoDate(time()+8*60*60); $res = $collection->aggregate(array( array( '$match' => array( 'uid' => array('$in' => $args['uid']), 'end' => array( '$gte' => $args['begin'], '$lte' => $args['end'] ) ) ), array( '$group' => array( '_id' => array('uid' => '$uid'), 'total' => array('$sum' => 1), 'rate' => array('$avg' => '$score_rate'), ) ), array( '$project' => array( 'rate' => '$rate', 'total' => '$total', 'uid' => '$_id.uid', '_id' => 0 ) ) )); $map = array(); if ($res['result']) { foreach ($res['result'] as $r) { $map[$r['uid']] = $r; } } return $map; } /** * 获取用户答题情况统计 * @param $args * @return array */ public function getUsersExerRecordStat($args) { /** @var EMongoClient $mongo */ $mongo = Yii::app()->mongodb; $collection = $mongo->selectCollection(WK::XT_EXER_RECORD_COLLECTION); // 参数 if (!is_array($args['uid'])) { $args['uid'] = (array)$args['uid']; } if ($args['uid']) { array_walk($args['uid'], function(&$uid){$uid = (int)$uid;}); } $args['begin'] = $args['begin'] ? new MongoDate($args['begin']/1000+8*60*60) : new MongoDate(strtotime('-1 year')); $args['end'] = $args['end'] ? new MongoDate($args['end']/1000+8*60*60) : new MongoDate(time()+8*60*60); $res = $collection->aggregate(array( array( '$match' => array( 'uid' => array('$in' => $args['uid']), 'date' => array( '$gte' => $args['begin'], '$lte' => $args['end'] ) ) ), array( '$group' => array( '_id' => array('uid' => '$uid'), 'total' => array('$sum' => 1), ) ), array( '$project' => array( 'total' => '$total', 'uid' => '$_id.uid', '_id' => 0 ) ) )); $map = array(); if ($res['result']) { foreach ($res['result'] as $r) { $map[$r['uid']] = $r; } } return $map; } /** * 统计每天答题场景数据 * @param $args * @return array */ public function statSceneAnswerEachDay($args) { /** @var EMongoClient $mongo */ $mongo = Yii::app()->mongodb; $collection = $mongo->selectCollection(WK::XT_EXAM_RECORD_COLLECTION); $res = $collection->aggregate(array( array( '$match' => array( 'date' => array( '$gte' => $args['begin'], '$lte' => $args['end'] ) ) ), array( '$project' => array( 'day' => array( '$dateToString' => array( 'format' => '%Y-%m-%d', 'date' => '$date' )), 'scene' => '$scene' ) ), array( '$group' => array( '_id' => array( 'day' => '$day', 'scene' => '$scene' ), 'total' => array('$sum' => 1) ) ), array( '$project' => array( '_id' => 0, 'day' => '$_id.day', 'sceneID' => '$_id.scene', 'total' => '$total', ) ) )); return (array)$res['result']; } /** * 统计每个学段答题场景数据 * @param $args * @return array */ public function statSceneAnswerEachSchType($args) { /** @var EMongoClient $mongo */ $mongo = Yii::app()->mongodb; $collection = $mongo->selectCollection(WK::XT_EXAM_RECORD_COLLECTION); $res = $collection->aggregate(array( array( '$match' => array( 'date' => array( '$gte' => $args['begin'], '$lte' => $args['end'] ) ) ), array( '$group' => array( '_id' => array( 'schooltype' => '$schooltype', 'scene' => '$scene' ), 'total' => array('$sum' => 1) ) ), array( '$project' => array( '_id' => 0, 'schoolTypeID' => '$_id.schooltype', 'sceneID' => '$_id.scene', 'total' => '$total', ) ) )); return (array)$res['result']; } ```