1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | @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; }<br> |
2、分组也可,
public void query1(){ String mondburl = Config.getInstance().getProperty("mongodb.host"); MongoClientOptions options = new MongoClientOptions.Builder().socketKeepAlive(true).build(); Mongo client = new MongoClient(mondburl, options); DB database = client.getDB("pnr"); DBCollection coll = database.getCollection("b_dep_pnr_flt"); //条件 DBObject dbObject = new BasicDBObject(); dbObject.put("alnCd", "CZ"); dbObject.put("fltNr", "3452"); dbObject.put("fltDt", "20180425000000"); dbObject.put("arvCd", "SZX"); dbObject.put("depCd", "LJG"); //FindIterable<Document> dbCursor = col.find((Bson) dbObject); /*创建 $match, 作用相当于query*/ DBObject match = new BasicDBObject("$match", dbObject); /* Group操作*/ DBObject groupFields = new BasicDBObject("_id",null); groupFields.put("MAXDcsTimestamp", new BasicDBObject("$max", "$dcsTimestamp")); DBObject group = new BasicDBObject("$group", groupFields); /* 查看Group结果 */ AggregationOutput output = coll.aggregate(match, group); // 执行 aggregation命令 Iterable<DBObject> results = output.results(); for (DBObject dbObject2 : results) { System.out.println(dbObject2.toString()); } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步