php - MongoDB Geospatial,项目匹配多点
问题描述
通过带有 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 是否能够仅投影匹配的多点而不是存储的整个集合?
解决方案
我能够解决这个问题:
- 在 $geoNear 管道之后,添加一个 $unwind 阶段以展开“includeLocs”数组,即 {$unwind: '$locations.coordinates'}(确保展开坐标)。
- 使用 $geoWithin 地理空间运算符为“$match”添加另一个阶段,该字段专门标记为展开坐标字段,即“$locations.coordinates”。
- 最后添加一个 '$group' 阶段以将所有内容加入 _id 字段。
推荐阅读
- linux - 执行写操作时内核中的空指针异常
- reactjs - ReactJS 将 props 从 root 传递给 child
- swift - 在swift 3中加载所有数据时如何在tableview中隐藏刷新控件?
- soap - SOAP 服务的 TAM 到 ISAM 迁移
- angular - Spring Boot 应用程序的架构
- c - 搜索并打印目录的所有文件和子文件夹
- javascript - 使用 jquery ajax、.net mvc 进行无限滚动
- java - 无法从静态上下文引用 Generis 方法出错
- angularjs - 力矩未定义角力矩选择器
- python - DjangOAuthToolkit - 如何为视图设置不同的读/写范围?