How do I perform a find using $geoIntersects / 2dsphere in an array subfield?

扶醉桌前 提交于 2019-12-11 01:27:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!