一个mongo聚合数据的例子
2017-04-18 16:00
489 查看
mongo里每天会记录一些这样的数据,要把这些数据按照时间聚合。
java里面每天凌晨定时执行这个函数即可
聚合之后的数据是这样的:
function statisticsSchoolForTimes(nowtime) { db.getCollection('all').aggregate( [ {$match:{"time":nowtime}}, { $group:{ _id:{"schoolId":"$schoolId","userRole":"$userRole"}, provinceCode:{$first:"$provinceCode"}, provinceValue:{$first:"$provinceValue"}, cityValue:{$first:"$cityValue"}, cityCode:{$first:"$cityCode"}, countyCode:{$first:"$countyCode"}, countyValue:{$first:"$countyValue"}, schoolLevel:{$first:"$schoolLevel"}, schoolName:{$first:"$schoolName"}, schoolId:{$first:"$schoolId"}, time:{$first:"$time"}, userRole:{$first:"$userRole"} } } ]).forEach(function(x){ var praiseNum=db.getCollection('all').find({"schoolId":x.schoolId,"time":x.time,"type":"praise","userRole":x.userRole}).count(); var downloadNum=db.getCollection('all').find({"schoolId":x.schoolId,"time":x.time,"type":"download","userRole":x.userRole}).count(); var collectionNum=db.getCollection('all').find({"schoolId":x.schoolId,"time":x.time,"type": "collection","userRole":x.userRole}).count(); var commentNum=db.getCollection('all').find({"schoolId":x.schoolId,"time":x.time,"type":"comment","userRole":x.userRole}).count(); var browseNum=db.getCollection('all').find({"schoolId":x.schoolId,"time":x.time,"type":"browse","userRole":x.userRole}).count(); db.getCollection('school').insert(x); db.getCollection('school').update({"schoolId":x.schoolId,"time":x.time,"userRole":x.userRole},{'$set': {'praiseNum': praiseNum,'downloadNum': downloadNum,'collectionNum': collectionNum,'commentNum': commentNum,'browseNum': browseNum}}); }) }
java里面每天凌晨定时执行这个函数即可
private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public void run() { try { System.out.println("开始数据统计聚合任务,时间为:"+formatter.format(Calendar.getInstance().getTime())); aggregate(); System.out.println("结束数据统计聚合任务,时间为:"+formatter.format(Calendar.getInstance().getTime())); } catch (Exception e) { System.out.println("-------------数据统计聚合任务发生异常--------------"+e.toString()); } }
public static void aggregate(){ System.out.println("开始数据统计聚合任务,时间为:"+formatter.format(Calendar.getInstance().getTime())); MongoUtils mu = new MongoUtils(); MongoUtils.createMongoClient(); DB db=mu.getDB(MongoUtils.DATABASE); String nowTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); db.eval("statisticsDistrictForTimes('"+nowTime+"')"); db.eval("statisticsSchoolForTimes('"+nowTime+"')"); mu.closeConnection(); System.out.println("结束数据统计聚合任务,时间为:"+formatter.format(Calendar.getInstance().getTime())); }
聚合之后的数据是这样的:
相关文章推荐
- Java8Map示例:一个略复杂的数据映射聚合例子及代码重构
- 重新认识表格和一个访问无障碍的数据表格例子
- 非阻塞方式下Socket读取数据的一个例子
- 一个jdbc的例子(包含sql语句的批处理,事务处理,数据绑定prepare)
- 一个select接收socket数据的例子
- Buffalo自动绑定表单Select数据的一个例子
- 一个用存储过程实现数据分页显示的例子,有更合适的请朋友们跟帖啊
- 哈哈,800万的数据终于导完了,晚上写了一个Template的小例子
- 一个通用的动态加载数据例子(IE5,NS6,Mozilla 1.3b)
- 非阻塞方式下Socket读取数据的一个例子
- 利用前台程序控制行转列数据的输出,可能从效率和扩展性上更好.一个例子
- 一个数据连接例子c#
- 一个使用JdbcTemplate增加一组数据的完整例子
- 一个使用GridView显示数据,并且可以进行添加、修改、删除操作的例子
- 非阻塞方式下Socket读取数据的一个例子
- datagrid数据绑定并格式化的一个例子
- [原创]一个利用PHP语言读取数据库数据的例子(菜鸟版)
- 一个jdbc的例子(包含sql语句的批处理,事务处理,数据绑定prepare,)
- 非阻塞方式下Socket读取数据的一个例子
- 一个类数据类型的STL例子