【问题标题】:$nearSphere query gives result java but how to calculate distance of each result?$nearSphere 查询给出结果 java 但如何计算每个结果的距离?
【发布时间】:2013-06-10 17:33:33
【问题描述】:

我在 mongoDB 中使用 $nearSphere 查询。我想要距离以及按距离排序的列表。

我想在显示结果的 javascript 中使用这个距离。我使用了几个公式来计算地球上各点之间的球面距离。没有给我 mongo 用来排序的确切距离? 有什么帮助吗?

【问题讨论】:

    标签: java javascript mongodb


    【解决方案1】:

    如果你使用$geoNear aggregation command,你也会得到计算出来的距离。

    我在今年的 presentation given at MongoDB London 中发现了这一点,它描述了 $geoNear 的使用(幻灯片 36 和 37),以及 MongoDB 中的其他地理特征。

    或者,您可以在以下一些演示文稿中了解一些 MongoDB 地理功能的工作原理:

    我确定您已经通过 Google 找到了这个,但 MongoDB 网站上有 documentation on Geospatial indexes/queries

    【讨论】:

      【解决方案2】:

      这实际上非常简单。要在 java 中添加,请创建如下查询 例如

      BasicDBObject distanceQuery = new BasicDBObject();
      BasicDBObject sphereQuery = new BasicDBObject();
      sphereQuery.put("$nearSphere",new Double[]{lat, lan});
      sphereQuery.put("$maxDistance", radiusInKm/6371);
      distanceQuery.put("point", sphereQuery);
      

      在响应中,您将获得一个“dist”参数,给出用于获取列表的确切距离。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-06
        • 2020-12-22
        相关资源
        最近更新 更多