[TOC]
# MongoDB #
*D:\wamp\www\xue.test\xuetang\protected\config\common.php*
```
'mongodb' => array(
'class' => 'EMongoClient',
'server' => 'mongodb://'.MONGO_HOST.':'.MONGO_PORT,
'db' => MONGO_DB,
'options' => array(
'connect' => true ,
//'username' => MONGO_USER,
// 'password' => MONGO_PWD ,
)
),
```
*D:\wamp\www\xue.test\xuetang\protected\service\service2\V2StatisticsService.php*
```
$mongo = Yii::app()->mongodb;
$collection = $mongo->selectCollection(WK::XT_EXAM_RECORD_COLLECTION);
// const XT_EXAM_RECORD_COLLECTION = 'xt_exam_record'; // 答卷记录
```
## MongoDB 聚合
MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果。有点类似sql语句中的 count(*)。
http://www.runoob.com/mongodb/mongodb-aggregate.html
```
$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
)
)
));
```
## 模型类 ##
*D:\wamp\www\xue.test\xuetang\protected\models\mongo\XtExamRecord.php*
~~~
<?php
/**
* Created by PhpStorm.
* User: Qmore
* Date: 16-8-16
* Time: 上午9:42
*/
class XtExamRecord extends EMongoDocument{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return XtExamRecord the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
function relations(){
return array(
'xtExerRecords' => array('many', 'XtExerRecord', 'aeid','on'=>'aeid')
);
}
/**
* Returns the collection name as a string
*
* @example
*
* return 'users';
*
* @return string
*/
public function collectionName(){
return WK::XT_EXAM_RECORD_COLLECTION;
}
}
~~~
## 测试结果
```
public function actionTest()
{
$recommends = wkeRecommend::model()->findAll('fdreferrerID=:uid',array('uid'=>Yii::app()->user->id));
if ($recommends) {
$studentIDs = array_map(function($r){return $r->fdUserID;}, $recommends);
}
$begin = '1547913600000';
$end = '1550505599999';
// 练习记录
$examRecords = V2StatisticsService::factory()->getUsersExamRecordStat([
'end' => $end,
'begin' => $begin,
'uid' => $studentIDs,
]);
p($examRecords);
// 过滤没有答题的学生,减少mongo查询
$availableUIDs = [];
foreach ($examRecords as $uid => $record) {
if ($record['total']) {
$availableUIDs[] = $uid;
}
}
// 答题记录
$exerRecords = [];
if ($availableUIDs) {
$exerRecords = V2StatisticsService::factory()->getUsersExerRecordStat([
'end' => $end,
'begin' => $begin,
'uid' => $availableUIDs,
]);
}
p($exerRecords);
}
```
结果:
```
array
(
474602 => array
(
'total' => 9
'rate' => 66.811111111111
'uid' => 474602
)
474598 => array
(
'total' => 7
'rate' => 57.385714285714
'uid' => 474598
)
)
---------------------------------------------
array
(
474602 => array
(
'total' => 117
'uid' => 474602
)
474598 => array
(
'total' => 66
'uid' => 474598
)
)
---------------------------------------------
```
![](./img/2019-02-18_153931.png)
## PHP操作MongoDB
```
$mongo = new MongoClient("mongodb://10.8.8.14:27017");
var_dump($mongo->getHosts()); //返回主机名称、端口号、健康程度、状态、ping耗时、前一次ping的时间戳
/*
array (size=1)
'10.8.8.14:27017;-;.;4676' =>
array (size=6)
'host' => string '10.8.8.14' (length=9)
'port' => int 27017
'health' => int 1
'state' => int 0
'ping' => int 1
'lastPing' => int 1550545309
*/
var_dump($mongo->listDBs()); //列出所有数据库,返回数据库名称、大小、是否为空以及总大小、ok状态
/*
array (size=3)
'databases' =>
array (size=3)
0 =>
array (size=3)
'name' => string 'admin' (length=5)
'sizeOnDisk' => float 143360
'empty' => boolean false
1 =>
array (size=3)
'name' => string 'local' (length=5)
'sizeOnDisk' => float 94208
'empty' => boolean false
2 =>
array (size=3)
'name' => string 'weike' (length=5)
'sizeOnDisk' => float 11886944256
'empty' => boolean false
'totalSize' => float 11887181824
'ok' => float 1
*/
$db = $mongo->weike; //选择数据库,返回数据库对象
var_dump($db->listCollections()); //返回全部的集合对象
var_dump($db->getCollectionNames());
/*
array (size=32)
0 => string 'teaching_activity_log' (length=21)
1 => string 'album_operation_log' (length=19)
2 => string 'jobtracker_data' (length=15)
3 => string 'admin_operation_log' (length=19)
4 => string 'xt_register_log' (length=15)
5 => string 'xt_login_log' (length=12)
6 => string 'xt_exam_record' (length=14)
7 => string 'tag_history' (length=11)
8 => string 'user_last_tag' (length=13)
9 => string 'exam_record' (length=11)
10 => string 'content_record' (length=14)
11 => string 'app_statistics' (length=14)
12 => string 'exam_cahe' (length=9)
13 => string 'exercise_basket' (length=15)
14 => string 'class_op_log' (length=12)
15 => string 'exer_record' (length=11)
16 => string 'system_message' (length=14)
17 => string 'operation_log' (length=13)
18 => string 'w_admin_op_log' (length=14)
19 => string 'action' (length=6)
20 => string 'dasai_system_message' (length=20)
21 => string 'pushNotification' (length=16)
22 => string 'kt_command_log' (length=14)
23 => string 'behavior_record' (length=15)
24 => string 'number_predict_history' (length=22)
25 => string 'xk_visit_log' (length=12)
26 => string 'activity_log' (length=12)
27 => string 'exam_cache' (length=10)
28 => string 'xt_exer_record' (length=14)
29 => string 'play_record' (length=11)
30 => string 'submit_exam_log' (length=15)
31 => string 'download_record' (length=15)
*/
$col = $db->selectCollection("xt_exam_record"); //选择表(集合),返回文档集合对象
$n = 0;
foreach ($col->find() as $item) {
echo "<pre>";
print_r($item);
echo "</pre>";
$n++;
if ($n > 10)
die;
}
/*
Array
(
[_id] => MongoId Object
(
[$id] => 5b57e68492e87e8e6f4bdc9c
)
[uid] => 1878564
[class] => Array
(
)
[grade] => 6
[school] => 18748
[city] => 1148
[dist] =>
[prov] => 8
[total] => 59
[schooltype] => 1
[alias] => 0
[book] =>
[subject] => 2
[e_eid] => 169477
[e_name] => 小学数学小数的简单运算考点练习
[aeid] => 1824709
[source] => 16
[total_obj] => 41
[rate] => 100
[score_rate] => 100
[date] => MongoDate Object
(
[sec] => 1532068205
[usec] => 0
)
[start] => MongoDate Object
(
[sec] => 1532068203
[usec] => 0
)
[end] => MongoDate Object
(
[sec] => 1532068259
[usec] => 0
)
[consume] => 33000
[scene] => 101
[status] => 1
[schedule] => Array
(
[0] => Array
(
[id] => 26
[tagid] => 53438
[level] => 3
)
[1] => Array
(
[id] => 25
[tagid] => 53437
[level] => 2
)
[2] => Array
(
[id] => 21
[tagid] => 53428
[level] => 1
)
)
[targetID] => 2
[score] => 59
[score_obj] => 0
)
*/
```
- 说明
- 开发任务
- 星课-真光
- 课表
- Excel Down
- 调课
- 课表修改
- 课表代码分析
- 课堂
- 课堂:应用商店通信管理协议
- 教师账号强制绑定手机或邮箱
- 强制绑定手机和修改密码的规则
- 学堂
- 课程学习:讨论功能
- 后台:课程讨论管理
- 课程直播接口
- 学习统计功能(旧版)
- 学习统计功能(新版)
- 同步课程统计功能
- 同步课程编辑-新增视频
- 第三方接口
- 学科网
- 安徽第三方
- 大赛
- 管控系统
- 日志管理
- 设备日志
- 平板接口
- 渝教
- 教学总结
- 空白目录
- Yii 1.1
- 学堂架构
- Yii 1.1一些方法的解读
- MVCS结构
- 基础使用语法
- 创建1个新模块
- 关联模型
- CDbCriteria
- 学生-课堂记录
- 学生端页面展示
- 教师端页面展示
- 编辑课程文档
- SQL
- 课堂项目运行入口
- 上传资源示意图
- 行为
- PHPStorm
- 源码阅读
- 会诊答卷页面
- 考点练习
- 资源首页
- 同步课程
- 同步课程:章节信息
- 升学复习
- 统计图-范例
- 模块
- 非法词
- 服务层
- MongoDB类
- 学堂作答记录从Mongo新集合获取数据
- MongoYii
- 错题集
- 小技巧
- 完善资料
- 邮件发送
- K12
- JSpang视频课程
- MongoDB
- 创业
- 项目
- 包包