mongodb多欄位分組統計並求最大統計失敗時間
一、普通求總數
db.sendlog.find({"event":1000000000001}).count()
二、分組統計並求最大時間
db.sendlog.group({ key:{template:"$template",event:"$event",channel:"$channel"},//根據mongodb中的欄位分組 initial: { failureNum:0,failureSendTime:"1990-01-01 00:00:00" },//初始化欄位 cond: { event: 1000000000001 ,success:"1"},//條件篩選 reduce: function ( curr, result) { result.failureNum++;//數量統計 if(curr.failureSendTime>result.failureSendTime){ result.failureSendTime=curr.failureSendTime;//求最大失敗時間 } } , finalize: function(result) { } } )
由於是公司資料庫,真正欄位就不顯示了,直接對應上方的欄位匹配
三、在java中用mongotemplate進行查詢
1.導包
<!--mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.寫配置
spring.data.mongodb.uri=mongodb://使用者名稱:密碼@IP:埠/資料庫名
例如:
spring.data.mongodb.uri=mongodb://user: [email protected]:27017/log
3.程式碼
@Autowired private MongoTemplate mongoTemplate; /** * 查詢統計傳送失敗資料 */ public List<MessageEventStatistic> querySuccessStatistic(Long eventId ) { Criteria criteria=Criteria.where("event").is(eventId).and("success").is("1"); GroupByResults<MessageEventStatistic> group = mongoTemplate.group( criteria, "msgSmsSendLog", new GroupBy("event","template", "channel") .initialDocument("{ failureNum: 0 ,failureSendTime:\"1990-01-01 00:00:00\"}") .reduceFunction("function(curr, result){ result.failureNum++; if(curr.failureSendTime>result.failureSendTime){ result.failureSendTime=curr.failureSendTime; }}"), MessageEventStatistic.class);//返回接收類,記得返回的欄位要和類的欄位一致 Iterator<MessageEventStatistic> iterator = group.iterator(); List<MessageEventStatistic> list=new ArrayList<>(); while(iterator.hasNext()){ MessageEventStatistic next = iterator.next(); next.setSuccessSendTime(DateUtil.parseTime(next.getSuccessSendTimeStr())); list.add(next); System.out.println(next.toString()); } return list; }
相關推薦
mongodb多欄位分組統計並求最大統計失敗時間
一、普通求總數 db.sendlog.find({"event":1000000000001}).count() 二、分組統計並求最大時間 db.sendlog.group({
Elasticsearch多欄位分組聚合, 並對分組聚合的count進行篩選
前提 假設我們的文件包括以下幾個欄位 : activityId, clientIp, orderNumber 目標 依據activityId(策略ID) + clientIp(IP地址)分組聚合, 查詢相同策略ID+相同IP下訂單數目超過2的聚合結果 實現 {
es java聚合方法——多欄位分組並求和
/** * 多欄位分組並求和;實現sql:select field1, field2, sum(field3) from table_name group by field1, field2; */ @Test public
Hive多欄位分組取Top N且保留某列不相同記錄
一、問題背景 1.先吐槽一下中國聯通自己的大資料開放能力平臺提供的計算叢集,Hive用的1.1,Spark用的1.5,Kafka0.8,我的天吶,原始的讓人抓狂,好多已經寫好的模型都要重寫...... 2.資料格式 第一列是device_number,第
Java8 stream 中利用 groupingBy 進行多欄位分組求和
Java8的groupBy實現集合的分組,類似Mysql的group by分組功能,注意得到的是一個map 對集合按照單個屬性分組 case1: List<String> items = Arrays.asList("apple", "apple", "banana"
預處理、const、static與sizeof-用#define實現宏並求最大值和最小值
最大 運算 code span sizeof stat eof 代碼 bsp 1:實現代碼: #define MAX(x,y) (((x)>(y)) ? (x):(y)) #define MIN(x,y) (((x)>(y)) ? (x):(y)) 需要註
80x86 組合語言程式設計:輸入數字串,排序、並求最大和最小
;從鍵盤輸入一個ENTER結束的十進位制數字串, 按ASCII碼值降序排列這個數字串,;舉報|2013-12-10 21:36鼬風行 | 分類:組合語言 | 瀏覽61次;並另起一行,顯示出ASCII碼
insert和 select 結合實現“插入某欄位在資料庫中的最大值+1”
本文是mysql 資料庫 問題一 將表一的資料匯入表二: 將表一的資料匯入表二: 將表二中的資料 插入到 表一,表一的列對應表二 select 出來的列 INSERT INTO 表一 (column1, column2, column3, c
Elasticsearch Aggregation 多個欄位分組統計 Java API實現
現有索引資料:index:schooltype:student---------------------------------------------------{"grade":"1", "class":"1", "name":"xiao 1"}{"grade":"1",
MySQL統計同一欄位不同值數量並根據另一欄位分組
1.這是一張人口表(person_info)的資訊,需要統計不同學歷男女的數量。 2.sql語句 SELECT xlhz,SUM(CASE xbdm WHEN 1 THEN 1 ELSE 0 E
PHP+MySQL對當月,當週,當日資料統計,並將相應欄位分組排序
在我們的開發過程中,往往會遇到對會員或者商家進行收益排行,分別按照當月,當週或者當日進行排序;當然,你可以先把用到的資料獲取到,然後再根據你想要的功能對資料進行相應處理。下面,我要給大家演示的是直接對MySQL進行操作,利用MySQL的一些統計函式,很方
EF多欄位求和(分組/不分組)
分組多欄位求和 query.GroupBy(q => new { q.Year, q.Month }) .Select(q => new { Year = q.Key.Year, Month = q.Key.Month, Bu
oracle利用分析函式row_number()over()查詢一張表所有欄位並按照其中部分欄位分組查詢某欄位最大值
先準備資料: deptid :部門id. parent_deptid :deptid 的父級部門,也就是depid 是他的子部門。 create table test_employee (empid int ,deptid int ,parent_deptid int,sa
mysql group_concat 分組,多欄位統一排序及輸出資料到java
本文不涉及mysql的配置,bean的建立和使用,資料庫連線等教程,僅介紹group_concat輸出到java 假設我們有如下資料 目標:輸出goodsId為1002,已顏色分組的資料。 首先編輯一下sql語句 String sql ="SEL
T-SQL對多個欄位去重並計數
定義和用法可以一同使用 DISTINCT 和 COUNT 關鍵詞,來計算非重複結果的數目。語法SELECT COUNT(DISTINCT column(s)) FROM table例子注意:下面的例子僅
怎樣對多個欄位去重並計數?
Distinct可以和Count 一起使用,去重並計數: COUNT({ DISTINCT expression}) 但是一起使用時,後面不能有多個欄位: //不允許的寫法:select count(distinct col1 , col2 , col3 ,......
mongoDB 按照時間欄位分組 【24小時,周,月,年】
最近在做mongoDB的統計, 有需求是 按照一天24小時分組, 按照一週的7天分組, 按照一個月30天分組, 按照一年12個月分組統計, mongodb 入庫的時間欄位是 2016-12-12 這樣的字串格式,並沒有用mongodb的格式UTC,有時差問題. 思路是,
java讀取mongoDB某個資料庫的集合的指定欄位值,並存入mysql資料庫指定表中
1、查詢mongodb單個欄位的所有值內容:db.getCollection('amac').find({},{"managerName":1,"_id":0}) //PS:"_id":0表示不顯示id值 java: //mongoDB資料讀取,存入MYSQL資料庫
SQL 兩個欄位分組,其中一個欄位的不同情形,分類並計算數量
查詢結果: SQL:select `dateTime`, sum(`result`=(case when result='勝' then '勝' end)) as '勝', sum(`result`=(case when result='負' then '負'
mongodb group by 多欄位/distinct等
aggregate [ [ '$match' => [ 'create_time' => [