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;
}

  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());
} }
上一篇:js2word/html2word的简单实现


下一篇:iPhone5停留在语音的界面,提示按三次home键,无法继续下去