kehu  
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());
        }
         
        }
复制代码

 

posted on   kehu  阅读(2597)  评论(0编辑  收藏  举报
努力加载评论中...
 
点击右上角即可分享
微信分享提示