# 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'];
}
```
- 说明
- 开发任务
- 星课-真光
- 课表
- Excel Down
- 调课
- 课表修改
- 课表代码分析
- 课堂
- 课堂:应用商店通信管理协议
- 教师账号强制绑定手机或邮箱
- 强制绑定手机和修改密码的规则
- 学堂
- 课程学习:讨论功能
- 后台:课程讨论管理
- 课程直播接口
- 学习统计功能(旧版)
- 学习统计功能(新版)
- 同步课程统计功能
- 同步课程编辑-新增视频
- 第三方接口
- 学科网
- 安徽第三方
- 大赛
- 管控系统
- 日志管理
- 设备日志
- 平板接口
- 渝教
- 教学总结
- 空白目录
- Yii 1.1
- 学堂架构
- Yii 1.1一些方法的解读
- MVCS结构
- 基础使用语法
- 创建1个新模块
- 关联模型
- CDbCriteria
- 学生-课堂记录
- 学生端页面展示
- 教师端页面展示
- 编辑课程文档
- SQL
- 课堂项目运行入口
- 上传资源示意图
- 行为
- PHPStorm
- 源码阅读
- 会诊答卷页面
- 考点练习
- 资源首页
- 同步课程
- 同步课程:章节信息
- 升学复习
- 统计图-范例
- 模块
- 非法词
- 服务层
- MongoDB类
- 学堂作答记录从Mongo新集合获取数据
- MongoYii
- 错题集
- 小技巧
- 完善资料
- 邮件发送
- K12
- JSpang视频课程
- MongoDB
- 创业
- 项目
- 包包