I’m running a geospatial query using a 2dsphere index on a field city_location that stores GeoJSON points:
db['collection'].find({
// some filters
"city_location": { $geoWithin: { $geometry: poly } }
})
Here’s an example of one such point that isn’t returned, even though it visually lies inside my polygon when plotted on geojson.io:
{ "type": "Point", "coordinates": [-86.851524, 21.161907] }
const poly = {
type: "Polygon",
coordinates: [[
[-117, 20.6],
[-117, 34.6],
[-79, 34.6],
[-79, 20.6],
[-117, 20.6]
]]
};```
If I modify the polygon to add intermediate points (for example, by adding midpoints along each edge) or use a very small polygon around the same area, the query works correctly:
const detailPoly = {
type: “Polygon”,
coordinates: [[
[-117, 20.6],
[-117, 27.6], // midpoint on west edge
[-117, 34.6],
[-98, 34.6], // midpoint on north edge
[-79, 34.6],
[-79, 27.6], // midpoint on east edge
[-79, 20.6],
[-98, 20.6], // midpoint on south edge
[-117, 20.6]
]]
};
const tinyPoly = {
type: “Polygon”,
coordinates: [[
[-86.9, 21.1], // Just around Cancun
[-86.9, 21.2],
[-86.8, 21.2],
[-86.8, 21.1],
[-86.9, 21.1]
]]
}```
Is this expected and if so is there a way to visualise how this geometry is being created and given I have a large surface, what is the recommended way to query for area that lies within it