具有可变距离的 MongoDb 近/近地查询 [英] MongoDb near/geonear query with variable distance

查看:18
本文介绍了具有可变距离的 MongoDb 近/近地查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行一个查询,其中距离是集合中的一个动态字段.

I would like to execute a query where the distance is a dynamic field from the collection.

集合中的条目示例:

{
  name:'myName',
  location: {lat:10, lng:20},
  maximumDistance: 10
}
{
  name:'myName2',
  location: {lat:20, lng:20},
  maximumDistance: 100
}

我的目标是从该集合中检索此 位置 靠近给定位置的所有元素,例如 (10,10) 但计算出的距离小于此 最大距离 字段.

My goal is to retrieve all the elements from this collection where this location is close to a given location, let's say (10,10) but where the calculated distance is less than this maximumDistance field.

我可以在 near 查询中将 maxDistance 设置为一个常量值(足够高以检索我想要的所有元素),然后在我的 java 代码中进行过滤,但我更愿意在查询中执行它可能太 SQL导向的.

I can set maxDistance to a constant value (high enough to retrieve all the elements I want) in the near query and then do the filter in my java code but I would prefer to do it on the query it is maybe too SQL oriented.

推荐答案

您将无法使用普通查询执行此操作,因为您无法动态设置每个文档的距离.从 MongoDB 2.4 开始,您可以使用聚合框架执行此操作,因为它们已将 geoNear 运算符添加到管道的开头.

You won't be able to do this with a normal query as you can't dynamically set the distance per document. As of MongoDB 2.4 you can do this with the aggregation framework as they have added the geoNear operator to the starts of pipelines.

第一阶段将是 geoNear,它与 geonear 命令非常相似.作为结果,我们还将得到从指定点(10,10)到文档的距离.

The first stage will be the geoNear which is very similar to the geonear command. We will also get the distance from the point specified (10,10) to the document as a result.

第二阶段,我们将需要使用项目运算符来广告 maximumDistance 字段和计算的 geoNear 距离之间的差异.

The second stage we will need to use the project operator to ad the different between the maximumDistance field and the computed geoNear distance.

最后,我们匹配那些具有正 delta ((max - distance) > 0) 的文档.

Lastly, we match those documents that have a positive delta ((max - distance) > 0).

这是使用 异步 Java 驱动程序的帮助类的管道.

Here is the pipeline using the Asynchronous Java Driver's helper classes.

package example;

import static com.allanbank.mongodb.builder.AggregationProjectFields.include;
import static com.allanbank.mongodb.builder.QueryBuilder.where;
import static com.allanbank.mongodb.builder.expression.Expressions.field;
import static com.allanbank.mongodb.builder.expression.Expressions.set;
import static com.allanbank.mongodb.builder.expression.Expressions.subtract;

import com.allanbank.mongodb.bson.element.ArrayElement;
import com.allanbank.mongodb.builder.Aggregate;
import com.allanbank.mongodb.builder.AggregationGeoNear;
import com.allanbank.mongodb.builder.GeoJson;

public class AggregateGeoNear {
    public static void main(String[] args) {
        Aggregate aggregate = Aggregate
                .builder()
                .geoNear(
                        AggregationGeoNear.builder()
                                .location(GeoJson.p(10, 10))
                                .distanceField("distance"))
                .project(
                        include("name", "location", "maximumDistance"),
                        set("delta",
                                subtract(field("maximumDistance"),
                                        field("distance"))))
                .match(where("delta").greaterThanOrEqualTo(0)).build();

        System.out
                .println(new ArrayElement("pipeline", aggregate.getPipeline()));
    }
}

这是创建的管道:

pipeline : [
  {
    '$geoNear' : {
      near : [
        10, 
        10
      ],
      distanceField : 'distance',
      spherical : false,
      uniqueDocs : true
    }
  }, 
  {
    '$project' : {
      name : 1,
      location : 1,
      maximumDistance : 1,
      delta : {
        '$subtract' : [
          '$maximumDistance', 
          '$distance'
        ]
      }
    }
  }, 
  {
    '$match' : {
      delta : { '$gte' : 0 }
    }
  }
]

HTH - 抢劫.

附:上面的构建器使用的是 1.2.0 版本的驱动程序的预发布版本.在我输入代码时,代码正在通过构建矩阵,应该会在 2013 年 3 月 22 日星期五之前发布.

P.S. The builders above are using a pre-release of the 1.2.0 version of the driver. The code is going through the build matrix as I type and should be released by Friday, March 22, 2013.

这篇关于具有可变距离的 MongoDb 近/近地查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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