您的位置:首页 > 编程语言 > Go语言

一个mongo聚合数据的例子

2017-04-18 16:00 489 查看
mongo里每天会记录一些这样的数据,要把这些数据按照时间聚合。



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()));
}


聚合之后的数据是这样的:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: