【问题标题】:$geoIntersects returns no results$geoIntersects 不返回任何结果
【发布时间】:2017-05-20 05:52:37
【问题描述】:

我正在查询一个多边形,以检查一个点是否在其中,但没有返回任何结果。我在mongo shell中:(MongoDB shell版本:3.2.6)

db.restPolygons.find();
{
  "_id" : ObjectId("586e663175c32828be59e3a9"),
  "zoneCoordinates" : {
    "type" : "Polygon",
    "coordinates" : [
      [ 2, 0 ],
      [ 6, 0 ],
      [ 6, 2 ],
      [ 2, 2 ],
      [ 2, 0 ]
    ]
  }
}

db.restPolygons.find({
  "zoneCoordinates": {
    "$geoIntersects": {
      "$geometry": {
        "type": "Point",
        "coordinates": [3 ,1]
      }
    }
  }
}).count();
    0

【问题讨论】:

  • minimongo 仅在流星客户端中,不执行 $geo - 您在服务器上的 mongo shell 中。
  • 将 [ ] 添加到坐标值,如下所示解决了问题,即使我在本地 mini mongodb shell 中使用它。谢谢。

标签: mongodb geospatial


【解决方案1】:

您的多边形无效,它在coordinates 字段中缺少一层封闭数组。这个应该可以工作:

> db.geo.find()
{
    "_id": ObjectId("586e663175c32828be59e3a9"),
    "zoneCoordinates": {
        "type": "Polygon",
        "coordinates": [
            [
                [2, 0],
                [6, 0],
                [6, 2],
                [2, 2],
                [2, 0]
            ]
        ]
    }
}

> db.geo.find({
    "zoneCoordinates": {
      "$geoIntersects": {
        "$geometry": {
          "type": "Point",
          "coordinates": [3 ,1]
        }
      }
    }
  }).count()
1

您可以在GeoJSONList 中检查您的 GeoJSON 对象的有效性

【讨论】:

    猜你喜欢
    • 2017-12-28
    • 2017-12-09
    • 2020-10-03
    • 2017-03-19
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多