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

查看:95
本文介绍了当前文档字段值内的地理空间$ 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.

NB 阅读 $geoNear 文档. "2dsphere"索引需要诸如"spherical"之类的选项,例如对于现实世界中的坐标应具有的选项.另外,可能需要应用"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天全站免登陆