db.XXBean.createIndex( { loc : "2dsphere" } )
# 创建成功提示
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
Query: { "datetime" : { "$gte" : "20180121000000" , "$lte" : "20180124120000"} , "station_id_c" : { "$in" : [ "54629" , "54734"]} , "loc" : { "$near" : { "$geometry" : { "type" : "Point" , "coordinates" : [ 118.0 , 38.0]} , "$maxDistance" : 50000.0}}}, Fields: { "station_name" : true , "gst" : true , "station_id_d" : true , "lat" : true , "lon" : true}, Sort: { }
/**
* 按时间范围、多站点/单站点、经纬度半径dis内多个站点检索地面要素
*
* @param begin
* 开始时间,必填 yyyyMMddHHmmss
* @param end
* 结束时间,必填 yyyyMMddHHmmss
* @param stationid
* 区站号(数字),Station_Id_c,可选,多选
* @param elements
* 要素,可选,多选
* @param lon
* 经度
* @param lat
* 纬度
* @param dis
* 半径
* @return
* @throws ParseException
*/
public List<XXBean> getXXInCircleByTimeRange(String begin, String end, String stationids, String elements,
String lon, String lat, String dis) 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 (lon != null && lat != null && dis != null) {
BasicDBObject loc = new BasicDBObject();
loc.append("$near",
new BasicDBObject()
.append("$geometry",
new BasicDBObject().append("type", "Point").append("coordinates",
new double[] { Double.parseDouble(lon), Double.parseDouble(lat) }))
.append("$maxDistance", Double.parseDouble(dis)));
dbObject.put("loc", loc);
}
Query query = new BasicQuery(dbObject, fieldsObject);
return mongoTemplate.find(query, XXBean.class, "XXBean");
}