【问题标题】:MongoDB Geospatial, project matching multi pointsMongoDB Geospatial,项目匹配多点
【发布时间】:2020-03-20 22:21:40
【问题描述】:

通过带有 Aggregate 的 MongoDB $geoNear 命令,我无法生成查询的 maxDistance 字段内的位置。

        array(
            '$geoNear'          => array(
                'near'             => array(
                    'type'            => "MultiPoint",
                    'coordinates'     => [ 
                        $city["geo"]["coordinates"][0], 
                        $city["geo"]["coordinates"][1] 
                    ],
                ),
                "distanceField"    => "distance",
                "maxDistance"      => 1*1609.34,
                "includeLocs"      => "locations",
                "spherical"        => true,
                "query" => array(
                    "status" => true
                ),
                "limit" => 500
            ),
        ),

文档声明使用 includeLocs “指定输出字段,用于标识用于计算距离的位置。当位置字段包含多个位置时,此选项很有用。”,但是 includeLocs 字段是包括所有多点位置。

mongodb 是否有能力只投影匹配的多点而不是存储的整个集合?

【问题讨论】:

  • 我认为你需要一个像GeoPHP 这样的库,我猜union 是使用的方法。
  • @WernfriedDomscheit 而不是使用库我可以在每个坐标上运行距离计算,但我认为 MongoDB 的“includeLocs”会返回细节......文档有点模糊。

标签: php mongodb geospatial mongodb-geospatial


【解决方案1】:

我能够解决这个问题:

  1. 在 $geoNear 管道之后,添加一个 $unwind 阶段以展开“includeLocs”数组,即 {$unwind: '$locations.coordinates'}(确保展开坐标)。
  2. 使用 $geoWithin 地理空间运算符为“$match”添加另一个阶段,其中字段专门标记为展开坐标字段,即“$locations.coordinates”。
  3. 最后添加一个“$group”阶段,将所有内容加入到 _id 字段中。

【讨论】:

    猜你喜欢
    • 2014-02-13
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多