1.根据mongo的geo索引,依据提供的经纬度信息算出附近的两公里内的店铺
val tradingArea = getConnection("shopTest") val myLocation = new BasicDBList() //纬度上的point要是double类型的数据 myLocation.put(0, "113.610008".toDouble) myLocation.put(1, "23.118244".toDouble) val geoNear = new BasicDBObject("$geoNear", new BasicDBObject("near", myLocation) .append("limit", 1) .append("distanceField", "centerCoordinates") .append("maxDistance", 2000 / 6378137)) println("near:" + geoNear) val areaId = tradingArea.aggregate(util.Arrays.asList(geoNear)) .useCursor(true) .iterator() .map(doc => { doc.getObjectId("_id").toString }).toStream.headOption getOrElse ("") areaId
关于geoNear
When using $geoNear, consider that:
- You can only use $geoNear as the first stage of a pipeline.
- You must include the distanceField option. The distanceField option specifies the field that will contain the calculated distance.
- The collection must have a geospatial index.
- The $geoNear requires that a collection have at most only one 2d index and/or only one 2dsphere index.
- You do not need to specify which field in the documents hold the coordinate pair or point. Because$geoNear requires that the collection have a single geospatial index, $geoNear implicitly uses the indexed field.
- If using a 2dsphere index, you must specify spherical: true.
- You cannot specify a $near predicate in the query field of the $geoNear stage.
Generally, the options for $geoNear are similar to the geoNear command with the following exceptions:
- distanceField is a mandatory field for the $geoNear pipeline operator; the option does not exist in the geoNear command.
- includeLocs accepts a string in the $geoNear pipeline operator and a boolean in thegeoNear command.
1.使用$goNear只能在管道处理的开始第一个阶段进行
2.必须指定distanceField,该字段用来决定是否包含距离字段
3.$gonNear和geoNear命令比较相似,但是也有一些不同:distanceField在$geoNear中是必选的,而在geoNear中是可选的;includeLocs在$geoNear中是string类型,而在geoNear中是boolen类型。