- Reference >
- Operators >
- Query and Projection Operators >
- Geospatial Query Operators >
- $geoWithin
$geoWithin¶
-
$geoWithin¶ New in version 2.4:
$geoWithinreplaces$withinwhich is deprecated.The
$geoWithinoperator 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
$geoWithinoperator queries for inclusion in a GeoJSON polygon or a shape defined by legacy coordinate pairs.The
$geoWithinoperator does not return sorted results. As a result MongoDB can return$geoWithinqueries more quickly than geospatial$nearor$nearSpherequeries, which sort results.The
2dsphereand2dindexes both support the$geoWithinoperator.Changed in version 2.2.3:
$geoWithindoes 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
$geoWithinusing the$geometryoperator.For a polygon with only an exterior ring use following syntax:
Important
Specify coordinates in
longitude, latitudeorder.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
$geoWithinqueries, 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:
$geoWithinreplaces$withinin MongoDB 2.4.