简体   繁体   中英

$geoIntersects returns no results

I am querying a polygon, to check if a point is inside of it or not, but no results are returned. I am in the mongo shell: (MongoDB shell version: 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

Your polygon is invalid, it's missing a level of enclosing array in the coordinates field. This one should work:

> 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

You can check the validity of your GeoJSON object in GeoJSONList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM