当前文档字段值内的地理空间 $near [英] Geospatial $near within current document field value

查看:15
本文介绍了当前文档字段值内的地理空间 $near的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接受这个查询:

{ 'location' : { '$near' : [x,y], '$maxDistance' : this.field } }

我想为 $maxDistance 分配当前评估文档中指定字段的值.那可能吗?

I want to assign $maxDistance the value of the specified field from the current evaluated document. Is that possible?

推荐答案

是的,这是可能的.您只需使用 $geoNear 代替.当心陷阱并仔细阅读.

Yes it's possible. You just use $geoNear instead. Beware the catches and read carefully.

假设您的意图是存储诸如 "travelDistance" 之类的字段,以在文档上指示任何此类搜索必须在提供的距查询点的距离的范围内"才能有效.然后我们简单地使用 $redact:

Presuming that your intent to is to store a field such as "travelDistance" to indicate on the document that any such searches must be "within" that supplied distance from the queried point to be valid. Then we simply query and evaluate the condition with $redact:

db.collection.aggregate([
  { "$geoNear": {
    "near": { 
      "type": "Point",
      "coordinates": [x,y]
    },
    "spherical": true,
    "distanceField": "distance"
  }},
  { "$redact": {
    "$cond": {
      "if": { "$lte": [ "$distance", "$travelDistance" ] },
      "then": "$$KEEP",
      "else": "$$PRUNE"
    }
  }}
])

唯一的问题是$geoNear 就像 $near 首先只会返回一定数量的附近"文档.您可以使用选项进行调整,但与一般查询表单不同的是,这基本上保证了最终返回的结果将小于指定的最近"数字.

The only catch is that $geoNear just like $near will only return a certain number of documents "near" in the first place. You can tune that with the options, but unlike the general query form, this is basically guaranteeing that the eventual returned results will be less than the specified "nearest" numbers.

只要你意识到这一点,那么这是完全有效的.

As long as you are aware of that, then this is perfectly valid.

这实际上是处理半径内附近"的排位赛的一般方法.

It is in fact the general way to deal with qualifying what is "near" within a radius.

还要注意根据您存储坐标的方式的距离".作为旧坐标对,距离将以弧度为单位,您可能需要进行数学运算才能将其转换为公里或英里.

Also be aware of the "distance" according to how you have the coordinates stored. As legacy coordinate pairs the distances will be in radians which you will probably need to do the math to convert to kilometers or miles.

如果使用 GeoJSON,那么距离总是以米为单位,作为标准格式.

If using GeoJSON, then the distances are always considered in meters, as the standard format.

所有数学笔记都在文档中.

All the math notes are in the documentation.

注意阅读$geoNear 文档仔细."2dsphere" 索引需要诸如 "sphereal" 之类的选项,例如您应该拥有的真实世界坐标.此外,可能需要应用 "limit" 以增加超过默认的 100 个文档结果,以便进一步修剪.

N.B Read the $geoNear documentation carefully. Options like "spherical" are required for "2dsphere" indexes, such as you should have for real world coordinates. Also "limit" may need to be applied to increase past the default 100 document result, for further trimming.

<小时>

当评论提到 spring mongo 时,这里基本上做了同样的事情:


As the comments mention spring mongo, then here is basically the same thing done for that:

Aggregation aggregation = newAggregation(
    new AggregationOperation() {
      @Override
      public DBObject toDBObject(AggregationOperationContext context) {
        return new BasicDBObject("$geoNear",
          new BasicDBObject(
            "near", new BasicDBObject(
              "type","Point")
              .append("coordinates", Arrays.asList(20,30))
            )
            .append("spherical",true)
            .append("distanceField","distance")
          );
        }
    },
    new AggregationOperation() {
      @Override
      public DBObject toDBObject(AggregationOperationContext context) {
        return new BasicDBObject("$redact",
          new BasicDBObject(
            "$cond", Arrays.asList(
               new BasicDBObject("$lte", Arrays.asList("$distance", "$travelDistance")),
               "$$KEEP",
               "$$PRUNE"
             )
          )
       );
     }
    }
);

这篇关于当前文档字段值内的地理空间 $near的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆