$geoWithin
Definition
The specified shape can be either a GeoJSON Polygon(either single-ringed or multi-ringed), a GeoJSONMultiPolygon, or a shape defined by legacy coordinatepairs. The $geoWithin
operator uses the $geometry
operator to specify the GeoJSON object.
To specify a GeoJSON polygons or multipolygons using the defaultcoordinate reference system (CRS), use the following syntax:
- {
- <location field>: {
- $geoWithin: {
- $geometry: {
- type: <"Polygon" or "MultiPolygon"> ,
- coordinates: [ <coordinates> ]
- }
- }
- }
- }
For $geoWithin
queries that specify GeoJSON geometries withareas greater than a single hemisphere, the use of the default CRSresults in queries for the complementary geometries.
New in version 3.0: To specify a single-ringed GeoJSON polygon with a custom MongoDB CRS, use the followingprototype that specifies the custom MongoDB CRS in the$geometry
expression:
- {
- <location field>: {
- $geoWithin: {
- $geometry: {
- type: "Polygon" ,
- coordinates: [ <coordinates> ],
- crs: {
- type: "name",
- properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
- }
- }
- }
- }
- }
The custom MongoDB CRS uses a counter-clockwise winding order andallows $geoWithin
to support queries with a single-ringedGeoJSON polygon whose area is greater thanor equal to a single hemisphere. If the specified polygon is smallerthan a single hemisphere, the behavior of $geoWithin
withthe MongoDB CRS is the same as with the default CRS. See also“Big” Polygons.
If querying for inclusion in a shape defined by legacy coordinatepairs on a plane, use the following syntax:
- {
- <location field>: {
- $geoWithin: { <shape operator>: <coordinates> }
- }
- }
The available shape operators are:
$box
,$polygon
,$center
(defines a circle), and$centerSphere
(defines a circle on a sphere).
Important
If you use longitude and latitude, specify coordinates in orderof longitude, latitude
.
Behavior
Geospatial Indexes
$geoWithin
does not require a geospatial index. However, ageospatial index will improve query performance. Both 2dsphere and 2d geospatial indexes support$geoWithin
.
Unsorted Results
The $geoWithin
operator does not return sorted results. Assuch, MongoDB can return $geoWithin
queries more quickly thangeospatial $near
or $nearSphere
queries, which sortresults.
Degenerate Geometry
$geoWithin
does not guarantee that it will consider a piece ofgeometry to contain its component geometry, or another polygon sharingits component geometry.
“Big” Polygons
For $geoWithin
, if you specify a single-ringed polygon thathas an area greater than a single hemisphere, include thecustom MongoDB coordinate reference system in the $geometry
expression; otherwise, $geoWithin
queries forthe complementary geometry. For all other GeoJSON polygons with areasgreater than a hemisphere, $geoWithin
queries for thecomplementary geometry.
Examples
Within a Polygon
The following example selects all loc
data that exist entirelywithin a GeoJSON Polygon. The area of the polygon isless than the area of a single hemisphere:
- db.places.find(
- {
- loc: {
- $geoWithin: {
- $geometry: {
- type : "Polygon" ,
- coordinates: [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ]
- }
- }
- }
- }
- )
For single-ringed polygons with areas greater than a single hemisphere,see Within a “Big” Polygon.
Within a “Big” Polygon
To query with a single-ringed GeoJSON polygon whose area is greaterthan a single hemisphere, the $geometry
expression mustspecify the custom MongoDB coordinate reference system. For example:
- db.places.find(
- {
- loc: {
- $geoWithin: {
- $geometry: {
- type : "Polygon" ,
- coordinates: [
- [
- [ -100, 60 ], [ -100, 0 ], [ -100, -60 ], [ 100, -60 ], [ 100, 60 ], [ -100, 60 ]
- ]
- ],
- crs: {
- type: "name",
- properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
- }
- }
- }
- }
- }
- )
Deprecated since version 2.4: $geoWithin
replaces $within
in MongoDB2.4.