'Find in which polygon certain coordinate (latitude, longitude) lies with MongoDB?

Let's say 100 documents (so to say polygons) in a collection named Areas with this structure:

{
  ...otherFields,
  commonName: "Butwal",
  area: {
    type: "Polygon",
    coordinates: [
      {
        latitude: 27.66779856,
        longitude: 83.4668095
      },
      ...
      ...
      {
        latitude: 27.66779856,
        longitude: "83.4668095
      }
    ]
  }
}

Now I would like to find in which of those document(so to say polygon) this coordinate lies:

{
  latitude: 27.683793,
  longitude: 83.458689
}

with the help of MongoDB either aggregation or other operators please help!!

OR if I can do with javascript(NodeJS) in very performance way

OR with some google maps API



Solution 1:[1]

{
    <location field>: {
        $geoWithin: {
            $geometry: {
                type: <"Polygon" or "MultiPolygon"> ,
                coordinates: [ <coordinates> ]
             }
         }
    }
}

For more information follow this link https://docs.mongodb.com/manual/reference/operator/query/geoWithin/

Solution 2:[2]

You can use $geoIntersects for this:

db.collection.find({
        geometry: {
          $geoIntersects: {
            $geometry: {type: 'Point', coordinates: inputCoordinates},
          },
        },
      })

To find the polygon document that includes the inputCoordinates point

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amit Kumawat
Solution 2 nimrod serok