Query a 2dsphere Index

The following sections describe queries supported by the 2dsphere index.

GeoJSON Objects Bounded by a Polygon

The $geoWithin operator queries for location data foundwithin a GeoJSON polygon. Your locationdata must be stored in GeoJSON format. Use the following syntax:

  1. db.<collection>.find( { <location field> :
  2. { $geoWithin :
  3. { $geometry :
  4. { type : "Polygon" ,
  5. coordinates : [ <coordinates> ]
  6. } } } } )

The following example selects all points and shapes thatexist entirely within a GeoJSON polygon:

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

Intersections of GeoJSON Objects

The $geoIntersects operator queries for locations thatintersect a specified GeoJSON object. A location intersects the objectif the intersection is non-empty. This includes documents that have ashared edge.

The $geoIntersects operator uses the following syntax:

  1. db.<collection>.find( { <location field> :
  2. { $geoIntersects :
  3. { $geometry :
  4. { type : "<GeoJSON object type>" ,
  5. coordinates : [ <coordinates> ]
  6. } } } } )

The following example uses $geoIntersects to select allindexed points and shapes that intersect with the polygon defined by thecoordinates array.

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

Proximity to a GeoJSON Point

Proximity queries return the points closest to the defined point andsorts the results by distance. A proximity query on GeoJSON datarequires a 2dsphere index.

To query for proximity to a GeoJSON point, use either the$near operator. Distance is in meters.

The $near uses the following syntax:

  1. db.<collection>.find( { <location field> :
  2. { $near :
  3. { $geometry :
  4. { type : "Point" ,
  5. coordinates : [ <longitude> , <latitude> ] } ,
  6. $maxDistance : <distance in meters>
  7. } } } )

For examples, see $near.

See also the $nearSphere operator and the:pipeline:_$geoNear_aggregation pipeline stage.

Points within a Circle Defined on a Sphere

To select all grid coordinates in a “spherical cap” on a sphere, use$geoWithin with the $centerSphere operator.Specify an array that contains:

Use the following syntax:

  1. db.<collection>.find( { <location field> :
  2. { $geoWithin :
  3. { $centerSphere :
  4. [ [ <x>, <y> ] , <radius> ] }
  5. } } )

The following example queries grid coordinates and returns alldocuments within a 10 mile radius of longitude 88 W and latitude30 N. The example converts the distance, 10 miles, to radians bydividing by the approximate equatorial radius of the earth, 3963.2 miles:

  1. db.places.find( { loc :
  2. { $geoWithin :
  3. { $centerSphere :
  4. [ [ -88 , 30 ] , 10 / 3963.2 ]
  5. } } } )