mongodb计算地理空间距离实例

本文介绍如何在MongoDB中进行地理空间距离计算,以解决在MySQL中此类计算CPU消耗高的问题。通过实例展示了从之前的$match聚合操作转换为使用geoNear查询,以获取更准确的附近信息查询结果。
摘要由CSDN通过智能技术生成
[size=large]需求:有很多条件,其中包括根据附近多少米查找信息,这个需要先定位当前人所在的位置(即经纬度),然后在进行这个经纬度的附近进行查找信息,因为之前根据经纬度算地理空间位置是在mysql中进行的,但是发现在mysql中计算很耗CPU,现特改成mongodb来做,mongodb不支持union all功能,所以mongodb还是不满足[/size]:

//判断是否按距离排序
if(seq == 1 || seq == 0 || seq == 4){
//默认排序统一采用综合排序
if(seq == 0){
seq = 4;
}
if(lon != 0 && lat != 0){
}else{
seq=2;
}
}
DBObject sortFileds = new BasicDBObject();
DBObject geoNearFileds = new BasicDBObject();
geoNearFileds.put("distanceMultiplier", r);
geoNearFileds.put("near", new double[]{lon,lat});
geoNearFileds.put("spherical",true);
geoNearFileds.put("distanceField", "distance");
geoNearFileds.put("includeLocs", "position");
geoNearFileds.put("query", criteria.getCriteriaObject());
if (nearby!=0) {//选择了附近
geoNearFileds.put("maxDistance", nearby);
if (seq==0||seq==4) {
sortFileds.put("seqScore", -1);
sortFileds.put("evalGoodCount", -1);
sortFileds.put("distance", 1);
if (nearby == 0) {
sortFileds.put("seqRating", 1);
}
sortFileds.put("carNo", 1);
}
sortFileds = sort(seq, sortFileds);
}else{//未选择附近
if (seq==0||seq==4) {
sortFileds.put("seqRating", -1);
sortFileds.put("carNo", 1);
sortFileds.put("distance", 1);
}else {
sortFileds = sort(seq, sortFileds);
}
}

DBObject geoNear = new BasicDBObject("$geoNear", geoNearFileds);

DBObject sort = new BasicDBObject("$sort", sortFileds);

//DBObject match = new BasicDBObject("$match", criteria.getCriteriaObject());

DBObject skip = new BasicDBObject("$skip",(pageNum-1)*pageSize);

DBObject limit = new BasicDBObject("$limit",pageSize);

List<DBObject> dbObjects = super.aggregate(CarInfo.class, geoNear/*,match*/,skip,limit,sort);

List<CarInfo> carInfos = new ArrayList<CarInfo>();

if (CollectionUtils.isEmpty(dbObjects)) {
return null;
}
for (DBObject dbObject : dbObjects) {
CarInfo carInfo = new CarInfo();
carInfo = MongoUtils.dbObject2Bean(dbObject, carInfo);
carInfo.setId(dbObject.get("_id").toString());
if (tag==1) {
carInfo.setCoverPic("1"+carInfo.getCoverPic());
}else {
carInfo.setCoverPic("2"+carInfo.getCoverPic());
}
carInfo.setEvalCount((Integer.valueOf(dbObject.get("evalGoodCount")+"")+Integer.valueOf(dbObject.get("evalMediumCount")+"")+Integer.valueOf(dbObject.get("evalBadCount")+""))+"");
carInfos.add(carInfo);
}

这段代码之前使用aggregate $match来进行根据条件查询,发现查询的结果跟实际结果差距很大,至今不知道为何,现改成geoNear的query匹配条件进行查询,结果正确
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值