Query: { "datetime" : { "$gte" : "20180121000000" , "$lte" : "20180124120000"} , "station_id_c" : { "$in" : [ "54831" , "54832"]} , "admin_code_chn" : { "$in" : [ { "$regex" : "^3707.*$" , "$options" : "i"} , { "$regex" : "^37070602.*$" , "$options" : "i"} , { "$regex" : "^3707.*$" , "$options" : "i"}]}}, Fields: { "station_name" : true , "gst" : true , "station_id_d" : true , "lat" : true , "lon" : true}, Sort: { }
/**
* 按时间范围、多站点/单站点、区域内多个站点检索地面要素
*
* @param begin
* 开始时间,必填 yyyyMMddHHmmss
* @param end
* 结束时间,必填 yyyyMMddHHmmss
* @param stationid
* 区站号(数字),Station_Id_c,可选,多选
* @param elements
* 要素,可选,多选
* @param areaids
* 区域ID
* @return
* @throws ParseException
*/
public List<XXBean> getXXInAreaIdByTimeRange(String begin, String end, String stationids, String elements,
String areaids) throws ParseException{
DBObject dbObject = new BasicDBObject();
dbObject.put("datetime", new BasicDBObject("$gte", begin).append("$lte", end));
if (stationids != null) {
String[] stationid = stationids.split(",");
dbObject.put("station_id_c", new BasicDBObject("$in", stationid));
}
BasicDBObject fieldsObject = null;
if (elements != null) {
fieldsObject = new BasicDBObject();
String[] param = elements.split(",");
for (int i = 0; i < param.length; i++) {
fieldsObject.put(param[i], true);
}
}
if (areaids != null) {
String[] areaid = areaids.split(",");
List<Pattern> patterns = Lists.newArrayList();
for(String area :areaid) {
if(area.endsWith("0000")) {
Pattern pattern = Pattern.compile("^" + area.substring(0, 2) + ".*$", Pattern.CASE_INSENSITIVE);
patterns.add(pattern);
}else if(area.endsWith("00")) {
Pattern pattern = Pattern.compile("^" + area.substring(0, 4) + ".*$", Pattern.CASE_INSENSITIVE);
patterns.add(pattern);
}else {
Pattern pattern = Pattern.compile("^" + area + ".*$", Pattern.CASE_INSENSITIVE);
patterns.add(pattern);
}
}
dbObject.put("admin_code_chn", new BasicDBObject("$in", patterns));
}
Query query = new BasicQuery(dbObject, fieldsObject);
return mongoTemplate.find(query, XXBean.class, "XXBean");
}