I have a MongoDB database that has a collection called fooCollection . This collection contains documents containing geospatial data on the path of a bounded polygon. I am using the C # MongoDB driver in my application. I noticed that he does not find documents with specific spatial queries, although he works with most of them. I emptied the collection, with the exception of one offensive document, and I tried to find it by doing the queries directly.
My document is as follows:
{ "_id" : UUID("12345678-62d9-4024-86dc-123456789012"), "polygon" : { "type" : "Polygon", "coordinates" : [ [ [ 18.414846, -33.9699577 ], [ 18.414846, -26.0991189 ], [ 31.0330578, -26.0991189 ], [ 31.0330578, -33.9699577 ], [ 18.414846, -33.9699577 ] ] ] }, "foo": "bar" }
I also have this index in this collection:
[ 1, { "polygon" : "2dsphere" }, "polygon_2dsphere", "data.fooCollection", 3 ]
The following query correctly returns this document:
db.getCollection('fooCollection').find( { "polygon": { $geoIntersects: { $geometry: { type: "LineString", coordinates: [[24.7698287, -28.7353533],[28.0423, -26.19793]] }}}})
However, this request does not execute:
db.getCollection('fooCollection').find( { "polygon": { $geoIntersects: { $geometry: { type: "LineString", coordinates: [[27.7706902, -26.1091189],[28.0423, -26.19793]] }}}})
If you build these three geometries, I cannot understand why you need to work, and not another.
- Both lines lie entirely inside the polygon.
- The working line is much longer
- The working line has a bearing between 0 ° and 90 °, and the other between 90 ° and 180 °.
Can anyone explain this behavior?
EDIT: I also tested individual points instead of using LineStrings. The only hit is [24.7698287, -28.7353533] . I need LineStrings - the request should be a hit, even if only the edge intersects the polygon, and the points do not lie inside
Here you can see geojson or you can build three geometries yourself by inserting the following line into http://geojson.io/ :
{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[18.414846,-33.9699577],[18.414846,-26.0991189],[31.0330578,-26.0991189],[31.0330578,-33.9699577],[18.414846,-33.9699577]]]},{"type":"LineString","coordinates":[[27.7706902,-26.1091189],[28.0423,-26.19793]]},{"type":"LineString","coordinates":[[24.7698287,-28.7353533],[28.0423,-26.19793]]}]}