Mongodb : Check if a point is inside a stored polygon
You must store your location data like this schema:
{"loc": {"coordinates":[ [ [1.0,1.0], [1.0,10.0], [10.0,10.0], [10.0,1.0], [1.0,1.0] ] ], "type":"Polygon" }}
and then send $geoIntersects
queries
db.polygons.find({"loc":{"$geoIntersects":{"$geometry":{"type":"Point", "coordinates":[x, y]}}}}
You have to use $geoIntersects
for that.
db.features.find({mystoredpolygon:{$geoIntersects:{$geometry:{type:'Point', coordinates:[22,38]}}}})
Yes, you can use $geoWithin
for the check, provided you define the countries as polygons using GeoJSON
. For that, try this GitHub repo.