问题
I have a collection with documents and each has a field array with polygons.
I want to test if my polygon geoIntersects any of those polygons. They are all box-shaped if it helps.
I will add bounty points if you add in the possibility of having my polygon in that array I'm testing. I wouldn't want it intersect with itself...
Cities:
{_id, buildings:[ {coo:{shape:"Polygon", coordinates:[ [0,0], [4,0], [4,5], [5,0],[0,0] ] }, {coo:{shape:"Polygon", coordinates:[ [0,0], [4,0], [4,5], [5,0],[0,0] ] } ] }}
Imagine I have around 100 or so elements in the array. I want to determine if, for a given shape, that might be included inside the array, if it intersects any or none of the other polygons (again, excluding itself it is in there).
回答1:
This page will help you make the geospatial index on your compound spatial documents:
http://docs.mongodb.org/manual/core/geospatial-indexes/
And then you can use this page to figure out your query
http://docs.mongodb.org/manual/reference/operator/box/#op._S_box
回答2:
try this :
<collection>.findOne({ 'polygons': { $geoIntersects: { $geometry: { type: "Point", coordinates: [Longitude, Latitude] } } } }
来源:https://stackoverflow.com/questions/16846310/how-do-i-perform-a-find-using-geointersects-2dsphere-in-an-array-subfield