public Map<String,Map<Integer,JSONObject>> queryMessage(List ipId) {
DBObject query1 = new BasicDBObject();
BasicDBObject fieldObject = new BasicDBObject();
fieldObject.put("themeCode",true);
fieldObject.put("latest",true);
fieldObject.put("message",true);
fieldObject.put("ipId",true);
query1.put(IPID,new BasicDBObject("$in", ipId));
Query query2 = new BasicQuery(query1,fieldObject);
DBCursor dbCursor =mongoJsdTemplate.getCollection("eagle_jsd_riskcon_third").find(query1);
Map messageOneMap = Maps.newHashMap();
Map messageTwoMap = Maps.newHashMap();
while (dbCursor.hasNext()){
DBObject object=dbCursor.next();
String themeCode = Func.isEmpty(object.get("themeCode"))?"":object.get("themeCode").toString();
Integer latest = Func.isEmpty(object.get("latest"))?1:Integer.valueOf(Func.isEmpty(object.get("latest"))?"":object.get("latest").toString());
JSONObject message = Func.isEmpty(object.get("message"))?null:JSONObject.parseObject(object.get("message").toString());
Integer ipIdd = Func.isEmpty(object.get("ipId"))?0:Integer.valueOf(Func.isEmpty(object.get("ipId"))?"":object.get("ipId").toString());
if (Func.equals(themeCode,"RIM00135") && Func.equals(latest ,1)){
if(Func.isNotEmpty(message)){
messageOneMap.put(ipIdd , message);
}
}
if (Func.equals(themeCode,"RIM00136") && Func.equals(latest ,1)){
if(Func.isNotEmpty(message)){
messageTwoMap.put(ipIdd , message);
}
}
Map<String,Map<Integer,JSONObject>> returnMap = Maps.newHashMap();
returnMap.put("RIM00135",messageOneMap);
returnMap.put("RIM00136",messageTwoMap);
// System.out.println("returnMap大小MessageOne" + returnMap.get("RIM00135").size());
// System.out.println("returnMap大小MessageTwo" + returnMap.get("RIM00136").size());
return returnMap;
}
复制代码
MongoCursor,他是mongo的游标,他其实并没有真正的查询到结果,相当于懒加载,
在调用find时,MongoDB shell并不立即查询数据库,而是在等待真正开始获取数据时才发送查询。(类似Linq中IQueryable),你可以通过游标来对最终结果进行控制