日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

mongodb按照时间分组统计

發(fā)布時(shí)間:2024/4/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mongodb按照时间分组统计 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用spring data mongodb v1.8

需求1、

數(shù)據(jù)結(jié)構(gòu)如下。說(shuō)明:改集合記錄的是公司各個(gè)系統(tǒng)的訪問(wèn)情況(localPath表示系統(tǒng),requestTime 表示請(qǐng)求時(shí)間,字符串類(lèi)型,)

需求:選中某一天,然后按照系統(tǒng)和小時(shí)進(jìn)行分組,統(tǒng)計(jì)這一天中每個(gè)小時(shí)各個(gè)系統(tǒng)的訪問(wèn)情況。

業(yè)務(wù)代碼:

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)))); //?匹配查詢(xún) MatchOperation?matchOperation?=?Aggregation.match(criteria);//?localPath //?返回參數(shù) ProjectionOperation?return1?=?Aggregation.project("localPath").andExpression("substr(requestTime,11,2)").as("dimension"); //?按條件分組 GroupOperation?go2?=?Aggregation.group("dimension",?"localPath").count().as("times"); //?設(shè)置排序 SortOperation?sortOperation?=?Aggregation.sort(Sort.Direction.ASC,?"localPath",?"dimension"); //?構(gòu)建參數(shù) Aggregation?aggregation?=?Aggregation.newAggregation(matchOperation,?return1,?go2,?sortOperation); //?分組聚合查詢(xún) AggregationResults<SysCountResultVo>?aggregate?=?mongoTemplate.aggregate(aggregation,?getCollectionName(date),?SysCountResultVo.class); //?獲取結(jié)果 List<SysCountResultVo>?resultVos?=?aggregate.getMappedResults();

SysCountResultVo類(lèi)

public?class?SysCountResultVo?{/**次數(shù)**/private?int?times;/**服務(wù)**/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?+?"]";}}

需求二、

數(shù)據(jù)結(jié)構(gòu)如下。說(shuō)明:該集合存儲(chǔ)的是投遞信息(isView 表示的類(lèi)型,deliveryTime 表示 投遞時(shí)間)

需求:

選擇一個(gè)時(shí)間段,要求按天和投遞類(lèi)型進(jìn)行分組統(tǒng)計(jì)。

難點(diǎn):

mongodb存儲(chǔ)的時(shí)間是utc時(shí)間,這個(gè)時(shí)間比本地時(shí)間少8個(gè)小時(shí),例如 本地時(shí)間為:2018.7.18 00:00:00日,mongodb中存儲(chǔ)的是 2018.7.17 t16:00:00z之類(lèi)的東西。

按天統(tǒng)計(jì)的時(shí)候,如果集合中存了字符處,可以使用需求1中的方法,使用substr函數(shù)進(jìn)行處理。但是如果像需求2中,沒(méi)有存儲(chǔ)字符串,怎么辦?以下是我的思路。

service

Criteria?criteria?=?new?Criteria();/**此處設(shè)置匹配條件**///?匹配查詢(xún)MatchOperation?matchOperation?=?Aggregation.match(criteria);//?返回參數(shù),對(duì)日期進(jìn)行處理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");//?設(shè)置排序SortOperation?sortOperation?=?Aggregation.sort(Sort.Direction.ASC,?"times");//?構(gòu)建參數(shù)Aggregation?aggregation?=?Aggregation.newAggregation(matchOperation,?return1,?go2,?sortOperation);//?分組聚合查詢(xún)AggregationResults<SysCountResultVo>?aggregate?=?mongoTemplate.aggregate(aggregation,?"resumeDeliveryRecordVo",?SysCountResultVo.class);//?獲取結(jié)果List<SysCountResultVo>?resultVos?=?aggregate.getMappedResults();

SysCountResultVo類(lèi)

public?class?SysCountResultVo?{/**次數(shù)**/private?int?times;/**服務(wù)**/private?String?isView;/**年**/private?Integer?year;/**月**/private?Integer?month;/**日**/private?Integer?day;/**小時(shí)**/private?Integer?hour;/**時(shí)間**/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;}//?此處為重點(diǎn),如果時(shí)>=16,則認(rèn)為是下一天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();}}

可能說(shuō)明的不是很清楚,不過(guò)可以作為一個(gè)參考。我的QQ:1208576787,如有什么問(wèn)題,可以加QQ討論下。

轉(zhuǎn)載于:https://blog.51cto.com/fengcl/2146887

總結(jié)

以上是生活随笔為你收集整理的mongodb按照时间分组统计的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。