$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 $geometryoperator to specify the GeoJSON object. To specify a GeoJSONpolygons or multipolygons using the default coordinate referencesystem (CRS), use the following syntax:

  1. {
  2. <location field>: {
  3. $geoIntersects: {
  4. $geometry: {
  5. type: "<GeoJSON object type>" ,
  6. coordinates: [ <coordinates> ]
  7. }
  8. }
  9. }
  10. }

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:

  1. {
  2. <location field>: {
  3. $geoIntersects: {
  4. $geometry: {
  5. type: "Polygon" ,
  6. coordinates: [ <coordinates> ],
  7. crs: {
  8. type: "name",
  9. properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
  10. }
  11. }
  12. }
  13. }
  14. }

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 and 180, bothinclusive.
  • Valid latitude values are between -90 and 90, 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:

  1. db.places.find(
  2. {
  3. loc: {
  4. $geoIntersects: {
  5. $geometry: {
  6. type: "Polygon" ,
  7. coordinates: [
  8. [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ]
  9. ]
  10. }
  11. }
  12. }
  13. }
  14. )

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:

  1. db.places.find(
  2. {
  3. loc: {
  4. $geoIntersects: {
  5. $geometry: {
  6. type : "Polygon",
  7. coordinates: [
  8. [
  9. [ -100, 60 ], [ -100, 0 ], [ -100, -60 ], [ 100, -60 ], [ 100, 60 ], [ -100, 60 ]
  10. ]
  11. ],
  12. crs: {
  13. type: "name",
  14. properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
  15. }
  16. }
  17. }
  18. }
  19. }
  20. )