$polygon

Definition

  • $polygon
  • Specifies a polygon for ageospatial$geoWithin query on legacy coordinatepairs. The query returns pairs that are within the bounds of thepolygon. The operator does not query for GeoJSON objects.

To define the polygon, specify an array of coordinate points:

  1. {
  2. <location field>: {
  3. $geoWithin: {
  4. $polygon: [ [ <x1> , <y1> ], [ <x2> , <y2> ], [ <x3> , <y3> ], ... ]
  5. }
  6. }
  7. }

The last point is always implicitly connected to the first. You canspecify as many points, i.e. sides, as you like.

Important

If you use longitude and latitude, specify longitude first.

Behavior

The $polygon operator calculates distances using flat (planar)geometry.

Applications can use $polygonwithout having a geospatial index.However, geospatial indexes support much faster queries than theunindexed equivalents.

Only the 2d geospatial index supports the$polygon operator.

Example

The following query returns all documents that have coordinates thatexist within the polygon defined by [ 0 , 0 ], [ 3 , 6 ], and[ 6 , 0 ]:

  1. db.places.find(
  2. {
  3. loc: {
  4. $geoWithin: { $polygon: [ [ 0 , 0 ], [ 3 , 6 ], [ 6 , 0 ] ] }
  5. }
  6. }
  7. )