$geoIntersects
Definition
$geoIntersects
- Selects documents whose geospatial data intersects with a specifiedGeoJSON object; i.e. wherethe intersection of the data and the specified object is non-empty.
The $geoIntersects
operator uses the $geometry
operator to specify the GeoJSON object. To specify a GeoJSONpolygons or multipolygons using the default coordinate referencesystem (CRS), use the following syntax:
- {
- <location field>: {
- $geoIntersects: {
- $geometry: {
- type: "<GeoJSON object type>" ,
- coordinates: [ <coordinates> ]
- }
- }
- }
- }
For $geoIntersects
queries that specify GeoJSON geometrieswith areas greater than a single hemisphere, the use of the defaultCRS results 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>: {
- $geoIntersects: {
- $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 $geoIntersects
to support queries with asingle-ringed GeoJSON polygon whose area isgreater than or equal to a single hemisphere. If the specifiedpolygon is smaller than a single hemisphere, the behavior of$geoIntersects
with the MongoDB CRS is the same as with thedefault CRS. See also “Big” Polygons.
Important
If specifying latitude and longitude coordinates, list thelongitude first and then latitude:
- Valid longitude values are between
-180
and180
, bothinclusive. - Valid latitude values are between
-90
and90
, bothinclusive.
Behavior
Geospatial Indexes
$geoIntersects
uses spherical geometry.$geoIntersects
does not require a geospatial index. However, ageospatial index will improve query performance. Only the2dsphere geospatial index supports$geoIntersects
.
Degenerate Geometry
$geoIntersects
does not guarantee that it will consider apolygon to intersect with its own edges; its own vertices; or anotherpolygon sharing vertices or edges but no interior space.
“Big” Polygons
For $geoIntersects
, 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, $geoIntersects
queries forthe complementary geometry. For all other GeoJSON polygons with areasgreater than a hemisphere, $geoIntersects
queries for thecomplementary geometry.
Examples
Intersects a Polygon
The following example uses $geoIntersects
to select allloc
data that intersect with the Polygon defined bythe coordinates
array. The area of the polygon is less than thearea of a single hemisphere:
- db.places.find(
- {
- loc: {
- $geoIntersects: {
- $geometry: {
- type: "Polygon" ,
- coordinates: [
- [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ]
- ]
- }
- }
- }
- }
- )
For single-ringed polygons with areas greater than a single hemisphere,see Intersects a “Big” Polygon.
Intersects 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: {
- $geoIntersects: {
- $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" }
- }
- }
- }
- }
- }
- )