# 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
- 創業
- 項目
- 包包