使用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;}@Overridepublic 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;}@Overridepublic String toString() {return "time:" + getTime() + ";times:" + getTimes() + ";isView:" + getIsView();}}

可能说明的不是很清楚,不过可以作为一个参考。我的QQ:1208576787,如有什么问题,可以加QQ讨论下。

转载于:https://blog.51cto.com/fengcl/2146887

mongodb按照时间分组统计相关推荐

  1. [MongoDB] 按时间分组统计(任意时间段)

    统计任意时间段内和sum,avg等信息 记录样本 {"_id" : ObjectId("5a2a290320fc1abc16104c0c"),"net ...

  2. oracle不连续得时间如何分组,Oracle按不同时间分组统计的sql

    Oracle按不同时间分组统计的sql以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Oracle按不同时间分组统计的s ...

  3. mysql 分组统计 每天_MySQL按时间分组统计每天的数量

    直接上代码: select CREATED_DATE,COUNT(*) as day_count from tb where DATE_SUB(CURDATE(), INTERVAL 1 MONTH) ...

  4. mysql 分组统计 原则_mysql数据库分组统计--根据时间分组统计

    引言 环境上有一个实时表库,该表存储了一天24小时定时任务.马上打算进行软件升级,领导让分析一下,什么时间升级软件,对程序的影响最小. 这样就需要进行数据统计,按小时进行分组,统计出每个小时的运行任务 ...

  5. MongoDB 分组统计

    [摘要] MongoDB 在进行分组统计时如果面对一些比较复杂的计算情况,往往会遇到 shell 脚本过于复杂的问题.而集算器 SPL 语言,则因其有丰富的函数库及易用性恰好能弥补 Mongo 这方面 ...

  6. mongodb aggregate按日期分组统计及spring mongo实现

    如需转载请注明出处: mongodb aggregate按日期分组统计及spring mongo实现 实现的需求 传入毫秒级开始时间戳和结束的时间戳,根据当前状态currentStatus.statu ...

  7. java 对 mongoDB 分组统计操作 以及一些常用操作

    为什么80%的码农都做不了架构师?>>>    /*** * 功能描述:* * @author :xiaoyu 创建日期 :2014年2月19日 下午2:23:44* * @para ...

  8. MongoDb数组操作 - unwind解包、group分组统计、sort排序

    MongoDB统计文档(Document)的数组(Array)中的各个元素出现的次数 一,问题描述 [使用 unwind 操作符 "解包" Document 里面的Array中的每 ...

  9. python excel 分组统计一个时间段内的数据,如三个月内,但不限于某个时间起止,是任何三个月的长度内

    """ 分组统计一个时间段内的数据,如三个月内,但不限于某个时间起止,是任何三个月的长度内 """import pandas as pd i ...

最新文章

  1. 2021年大数据ELK(二十三):Kibana简介
  2. linux shell cgi post,linux下shell处理cgi的方法--post get
  3. 智能合约语言 Solidity 教程系列9 - 错误处理
  4. clover 主题_Clover主题更换
  5. 全国计算机机专业考试试题,2010全国非计算机专业一级考试试题
  6. isecure center 综合安防管理平台_企业综合安全管理平台
  7. 解决重启VCSA 6.0提示:503 Service Unavailable错误
  8. 【Vue2.0】—Vue监视数据的原理(五)
  9. c++求两点的距离利用友元_用c++定义两个坐标点,计算两点间距离;进而计算线段的面积...
  10. python cursor函数_执行从python返回cursor的db2plsql函数
  11. autocad型源代码_总结一下可以研究的CAD源代码
  12. 关闭windows开机浏览器自动跳转MSN
  13. WebView下载点击无反应问题
  14. java的程序员工资一般多少_JAVA程序员工资一般是多少
  15. 关于CAN总线的布线
  16. 微笑识别(HOG+SVM+opencv+python)
  17. 现代检测技术课程实验编程:最小二乘法应用编程
  18. Observability——Datadog
  19. oppo手机的屏幕常亮问题
  20. 抓包工具——IE浏览器HttpWatch插件

热门文章

  1. android edittext 不可编辑
  2. android Set的遍历
  3. 【Android】事件传递:向下拦截,向上处理
  4. Tensorflow2.0(Keras)转换TFlite
  5. 利用CSIDL打开特殊文件夹
  6. 通过数据库动态视图'v$',查看数据库信息
  7. D. Imbalanced Array
  8. Github 新学入门
  9. 解决iOS地图持续定位耗电问题
  10. poj 3083 DFS