Selects documents with geospatial data that exists entirely within a specified shape.
The specified shape can be either a GeoJSON Polygon (either single-ringed or multi-ringed), a GeoJSON MultiPolygon, or a shape defined by legacy coordinate pairs. The
$geoWithinoperator uses the
$geometryoperator to specify the GeoJSON object.
To specify a GeoJSON polygons or multipolygons using the default coordinate reference system (CRS), use the following syntax:
$geoWithinqueries that specify GeoJSON geometries with areas greater than a single hemisphere, the use of the default CRS results in queries for the complementary geometries.
The custom MongoDB CRS uses a counter-clockwise winding order and allows
$geoWithinto support queries with a single-ringed GeoJSON polygon whose area is greater than or equal to a single hemisphere. If the specified polygon is smaller than a single hemisphere, the behavior of
$geoWithinwith the MongoDB CRS is the same as with the default CRS. See also “Big” Polygons.
If querying for inclusion in a shape defined by legacy coordinate pairs on a plane, use the following syntax:
The available shape operators are:
If you use longitude and latitude, specify coordinates in order of
$geoWithin does not guarantee that it will consider a piece of geometry to contain its component geometry, or another polygon sharing its component geometry.
$geoWithin, if you specify a single-ringed polygon that has an area greater than a single hemisphere, include
the custom MongoDB coordinate reference system in the $geometry expression; otherwise,
$geoWithin queries for the complementary geometry. For all other GeoJSON polygons with areas greater than a hemisphere,
$geoWithin queries for the complementary geometry.
The following example selects all
loc data that exist entirely within a GeoJSON Polygon. The area of the polygon is less than the area of a single hemisphere:
For single-ringed polygons with areas greater than a single hemisphere, see Within a “Big” Polygon.