Query to Match a Polygon that contains a Point Query to Match a Polygon that contains a Point mongodb mongodb

Query to Match a Polygon that contains a Point


What you seem to be trying to do by matching array elements is actually "Finding a Polygon that contains a Point", which is why I changed your question title.

To accomplish this you are better off using the geoSpatail features of MongoDB rather than trying to work out the bounds yourself. With valid GeoJSON data the query is quite simple using the $geoIntersects operator.

To demonstrate, I'll first set up a collection, with some Polygon data:

db.areas.insert([    {        "name": "San Jose",        "geometry": {            "type": "Polygon",            "coordinates": [[                [ -122.20916748046876, 37.13404537126446  ],                [ -122.20916748046876, 37.496652341233364 ],                [ -121.65710449218749, 37.496652341233364 ],                [ -121.65710449218749, 37.13404537126446  ],                [ -122.20916748046876, 37.13404537126446  ]             ]]         }    },    {        "name": "San Franciso",        "geometry": {            "type": "Polygon",            "coordinates": [[                [ -122.73651123046874, 37.58811876638322 ],                [ -122.73651123046874, 37.89219554724437 ],                [ -122.28332519531249, 37.89219554724437 ],                [ -122.28332519531249, 37.58811876638322 ],                [ -122.73651123046874, 37.58811876638322 ]            ]]        }    }])

Then ( though not required for $geoIntersects specifically ) when working with geoSpatial data it is best to have an "index" defined. The one that makes sense for real GeoJSON locations is "2dsphere". The index is created on the field that contains the "root" of the GeoJSON data, which is in this case called "geometry":

db.areas.createIndex({ "geometry": "2dsphere" })

Then all you need to do is supply a .find() query. I'm using the coordinates for "San Francisco city" here:

db.areas.find({    "geometry": {        "$geoIntersects": {             "$geometry": {                "type": "Point",                "coordinates": [                     -122.45361328124999,                     37.76420119453823                ]             }         }    }})

Which of course returns the "Polyon" defining the "San Franciso" area since that "Point" lies within that object.

    {        "name": "San Franciso",        "geometry": {            "type": "Polygon",            "coordinates": [[                [ -122.73651123046874, 37.58811876638322 ],                [ -122.73651123046874, 37.89219554724437 ],                [ -122.28332519531249, 37.89219554724437 ],                [ -122.28332519531249, 37.58811876638322 ],                [ -122.73651123046874, 37.58811876638322 ]            ]]        }    }

That is all there is to finding whether your "Point" lies within a "Polygon" you have stored in your collection.

Also look at tools such as geojsonlint.com and geojson.io (examples, not endorsements) in order to validate and visualise your data, which from your example does not provide a well formed Polygon.