- Reference >
- Operators >
- Query and Projection Operators >
- Geospatial Query Operators >
- $geoWithin
$geoWithin¶
-
$geoWithin
¶ New in version 2.4:
$geoWithin
replaces$within
which is deprecated.The
$geoWithin
operator is a geospatial query operator that queries for a defined point, line or shape that exists entirely within another defined shape. When determining inclusion, MongoDB considers the border of a shape to be part of the shape, subject to the precision of floating point numbers.The
$geoWithin
operator queries for inclusion in a GeoJSON polygon or a shape defined by legacy coordinate pairs.The
$geoWithin
operator does not return sorted results. As a result MongoDB can return$geoWithin
queries more quickly than geospatial$near
or$nearSphere
queries, which sort results.The
2dsphere
and2d
indexes both support the$geoWithin
operator.Changed in version 2.2.3:
$geoWithin
does not require a geospatial index. However, a geospatial index will improve query performance.If querying for geometries that exist within a GeoJSON polygon on a sphere, pass the polygon to
$geoWithin
using the$geometry
operator.For a polygon with only an exterior ring use following syntax:
Important
Specify coordinates in
longitude, latitude
order.For a polygon with an exterior and interior ring use following syntax:
The following example selects all indexed points and shapes that exist entirely within a GeoJSON polygon:
If querying for inclusion in a shape defined by legacy coordinate pairs on a plane, use the following syntax:
For the syntax of shape operators, see:
$box
,$polygon
,$center
(defines a circle), and$centerSphere
(defines a circle on a sphere).Note
Any geometry specified with GeoJSON to
$geoWithin
queries, must fit within a single hemisphere. MongoDB interprets geometries larger than half of the sphere as queries for the smaller of the complementary geometries.
-
$within
¶ Deprecated since version 2.4:
$geoWithin
replaces$within
in MongoDB 2.4.