一个mongo聚合数据的例子

来源:互联网 发布:软件配置状态报告 编辑:程序博客网 时间:2024/05/02 01:05


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


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



1 0
原创粉丝点击