使用spring data mongodb v1.8
需求1、
数据结构如下。说明:改集合记录的是公司各个系统的访问情况(localPath表示系统,requestTime 表示请求时间,字符串类型,)
需求:选中某一天,然后按照系统和小时进行分组,统计这一天中每个小时各个系统的访问情况。
业务代码:
Criteria criteria = new Criteria(); criteria.andOperator(Criteria.where("createTime").lt(DateUtil.addDay(sdf.parse(sdf.format(date)), 1)), Criteria.where("createTime").gte(sdf.parse(sdf.format(date)))); // 匹配查询 MatchOperation matchOperation = Aggregation.match(criteria);// localPath // 返回参数 ProjectionOperation return1 = Aggregation.project("localPath").andExpression("substr(requestTime,11,2)").as("dimension"); // 按条件分组 GroupOperation go2 = Aggregation.group("dimension", "localPath").count().as("times"); // 设置排序 SortOperation sortOperation = Aggregation.sort(Sort.Direction.ASC, "localPath", "dimension"); // 构建参数 Aggregation aggregation = Aggregation.newAggregation(matchOperation, return1, go2, sortOperation); // 分组聚合查询 AggregationResults<SysCountResultVo> aggregate = mongoTemplate.aggregate(aggregation, getCollectionName(date), SysCountResultVo.class); // 获取结果 List<SysCountResultVo> resultVos = aggregate.getMappedResults();
SysCountResultVo类
public class SysCountResultVo { /**次数**/ private int times; /**服务**/ private String localPath; /**维度**/ private String dimension; public int getTimes() { return times; } public void setTimes(int times) { this.times = times; } public String getLocalPath() { return localPath; } public void setLocalPath(String localPath) { this.localPath = localPath; } public String getDimension() { return dimension; } public void setDimension(String dimension) { this.dimension = dimension; } @Override public String toString() { return "SysCountResultVo [times=" + times + ", localPath=" + localPath + ", dimension=" + dimension + "]"; } }
需求二、
数据结构如下。说明:该集合存储的是投递信息(isView 表示的类型,deliveryTime 表示 投递时间)
需求:
选择一个时间段,要求按天和投递类型进行分组统计。
难点:
mongodb存储的时间是utc时间,这个时间比本地时间少8个小时,例如 本地时间为:2018.7.18 00:00:00日,mongodb中存储的是 2018.7.17 t16:00:00z之类的东西。
按天统计的时候,如果集合中存了字符处,可以使用需求1中的方法,使用substr函数进行处理。但是如果像需求2中,没有存储字符串,怎么办?以下是我的思路。
service
Criteria criteria = new Criteria(); /**此处设置匹配条件**/ // 匹配查询 MatchOperation matchOperation = Aggregation.match(criteria); // 返回参数,对日期进行处理 ProjectionOperation return1 = Aggregation.project("isView").andExpression("year(deliveryTime)").as("year").andExpression("month(deliveryTime)").as("month").andExpression("dayOfMonth(deliveryTime)").as("day").andExpression("hour(deliveryTime)").as("hour"); // 按条件分组 GroupOperation go2 = Aggregation.group("isView", "year", "month", "day", "hour").count().as("times"); // 设置排序 SortOperation sortOperation = Aggregation.sort(Sort.Direction.ASC, "times"); // 构建参数 Aggregation aggregation = Aggregation.newAggregation(matchOperation, return1, go2, sortOperation); // 分组聚合查询 AggregationResults<SysCountResultVo> aggregate = mongoTemplate.aggregate(aggregation, "resumeDeliveryRecordVo", SysCountResultVo.class); // 获取结果 List<SysCountResultVo> resultVos = aggregate.getMappedResults();
SysCountResultVo类
public class SysCountResultVo { /**次数**/ private int times; /**服务**/ private String isView; /**年**/ private Integer year; /**月**/ private Integer month; /**日**/ private Integer day; /**小时**/ private Integer hour; /**时间**/ private String time; private String isViewStr; public int getTimes() { return times; } public void setTimes(int times) { this.times = times; } public String getIsView() { return isView; } public void setIsView(String isView) { this.isView = isView; } public Integer getYear() { return year; } public void setYear(Integer year) { this.year = year; } public Integer getMonth() { return month; } public void setMonth(Integer month) { this.month = month; } public Integer getDay() { return day; } public void setDay(Integer day) { this.day = day; } // 此处为重点,如果时>=16,则认为是下一天 public String getTime() { try { if (hour >= 16) { return DateUtil.date2DateStr(DateUtil.addDay(DateUtil.dateStr2Date(this.getYear() + "-" + this.getMonth() + "-" + this.getDay(), DateUtil.PATTERN_DTSHORTLINE), 1), DateUtil.PATTERN_DTSHORTLINE); } return this.getYear() + "-" + this.getMonth() + "-" + this.getDay(); } catch (Exception e) { e.printStackTrace(); } return null; } public Integer getHour() { return hour; } public void setHour(Integer hour) { this.hour = hour; } public void setTime(String time) { this.time = time; } public String getIsViewStr() { return Integer.valueOf(isView) == 1 ? "未查看" : Integer.valueOf(isView) == 2 ? "待沟通" : Integer.valueOf(isView) == 4 ? "已查看" : Integer.valueOf(isView) == 6 ? "不合适" : "其他"; } public void setIsViewStr(String isViewStr) { this.isViewStr = isViewStr; } @Override public String toString() { return "time:" + getTime() + ";times:" + getTimes() + ";isView:" + getIsView(); } }
可能说明的不是很清楚,不过可以作为一个参考。我的QQ:1208576787,如有什么问题,可以加QQ讨论下。
原文地址:http://blog.51cto.com/fengcl/2146887
时间: 2024-10-08 18:16:21