mongodb分组排序
Posted kehu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb分组排序相关的知识,希望对你有一定的参考价值。
@Override public MessageDto getCheckInMembersByFlight(String fltDt, String fltNr, String channel,String origincd,String destcd) { log.info("请求参数:"+"fltDt:"+fltDt+",fltNr:"+fltNr+",channel:"+channel+",origincd:"+origincd+",destcd:"+destcd); MessageDto messageDto = new MessageDto(); boolean flag = (null !=fltDt&&!("").equals(fltDt) &&null !=fltNr&&!("").equals(fltNr) &&null !=channel&&!("").equals(channel) &&null !=origincd&&!("").equals(origincd) &&null !=destcd&&!("").equals(destcd)) &&(channel.equals("WX")|channel.equals("APP")); if (flag) { String fltDtStr = fltDt.replace("-","")+"000000"; //分组的依据psgId DBObject keys = new BasicDBObject("psgId", true); //dbObject为检索条件,检索出符合业务的数据 BasicDBObject dbObject = new BasicDBObject(); dbObject.put("fltDt", fltDtStr); String fltNrSub = fltNr.substring(2); dbObject.put("fltNr", fltNrSub); dbObject.put("checkinStatus", "AC"); dbObject.put("originCd", origincd); dbObject.put("destCd",destcd ); //设定下面函数的初始值:prev的字段dcsTimestamp的初始值,用于做比较取dcsTimestamp的最大值 DBObject initial = new BasicDBObject("dcsTimestamp", "0"); //这个属于js的语法,这个脚本用于在psgId组内,比较出较大的dcsTimestamp,并且取出这条数据中要返回的值 //注意后面的赋值要有dcsTimestamp的赋值,不然得出的数据不准确 String reduce = "function(doc,prev){" + "var docDcsTimestamp=doc.dcsTimestamp;" + "var prevDcsTimestamp=prev.dcsTimestamp;" + "var docDcsTimestampInt=parseInt(docDcsTimestamp);" + "var prevDcsTimestampInt=parseInt(prevDcsTimestamp);" + "if(docDcsTimestampInt > prevDcsTimestampInt){" + "prev.fqNo=doc.fqNo;" + "prev.credentNo=doc.credentNo;" + "prev.chnName=doc.chnName;prev.dcsTimestamp=doc.dcsTimestamp;}" + "}"; BasicDBList dbList = (BasicDBList) mongoTemplate.getCollection("b_dep_pnr_psg").group(keys, dbObject, initial, reduce); log.info("分组处理成功"); JSONArray resultJson = new JSONArray(); for (int i = 0; i < dbList.size(); i++) { //取出最终的得到数据中的要返回的参数,封装到map转json返回。添加name和证件号不为空判断,其中一个为空不返回 JSONObject jsStr = JSONObject.parseObject(dbList.get(i).toString()); String fqNoStr = (String) jsStr.get("fqNo"); String chnName = (String) jsStr.get("chnName"); String credentNo = (String) jsStr.get("credentNo"); Map<String, String> map = new HashMap<String, String>(); ObjectMapper mapJson = new ObjectMapper(); if(null!=chnName&&!chnName.equals("")&&null!=credentNo&&!credentNo.equals("")){ map.put("fqNo", fqNoStr); map.put("name", chnName); map.put("credentNo", credentNo); }else{ continue; } String params = null; JSONObject resultJsonObiect = null; try { params = mapJson.writeValueAsString(map); resultJsonObiect = JSONObject.parseObject(params); } catch (JsonProcessingException e) { e.printStackTrace(); } resultJson.add(resultJsonObiect); } messageDto.setMessage("成功"); messageDto.setCode("0"); messageDto.setContent(resultJson); } else { messageDto.setMessage("参数个数不对"); messageDto.setCode("10001"); messageDto.setContent(null); } return messageDto; }
以上是关于mongodb分组排序的主要内容,如果未能解决你的问题,请参考以下文章
Mongodb:按元素分组并根据条件显示子文档计数并按日期对文档进行排序
mongodb 分组统计: 按deviceId、tenant分组,统计总记录条数、求和workload