- Reference >
- Operators >
- Query and Projection Operators >
- Geospatial Query Operators >
- $polygon
$polygon¶
-
$polygon¶ New in version 1.9.
The
$polygonoperator specifies a polygon for a geospatial$geoWithinquery on legacy coordinate pairs. The query returns pairs that are within the bounds of the polygon. The operator does not query for GeoJSON objects.The
$polygonoperator calculates distances using flat (planar) geometry.The
2dgeospatial index supports the$polygonoperator.To define the polygon, specify an array of coordinate points. Use the following syntax:
Important
If you use longitude and latitude, specify longitude first.
The last point specified is always implicitly connected to the first. You can specify as many points, and therefore sides, as you like.
The following query returns all documents that have coordinates that exist within the polygon defined by
[ 0 , 0 ],[ 3 , 6 ], and[ 6 , 0 ]:Changed in version 2.2.3: Applications can use
$polygonwithout having a geospatial index. However, geospatial indexes support much faster queries than the unindexed equivalents. Before 2.2.3, a geospatial index must exist on a field holding coordinates before using any of the geospatial query operators.