MongoDB aggregate

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类型。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值