OPTIONS

Geospatial Query Operators

Operators

Query Selectors

Name Description
$geoIntersects Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects.
$geoWithin Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin.
$nearSphere Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.
$near Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near.

Geometry Specifiers

Name Description
$box Specifies a rectangular box using legacy coordinate pairs for $geoWithin queries. The 2d index supports $box.
$centerSphere Specifies a circle using either legacy coordinate pairs or GeoJSON format for $geoWithin queries when using spherical geometry. The 2dsphere and 2d indexes support $centerSphere.
$center Specifies a circle using legacy coordinate pairs to $geoWithin queries when using planar geometry. The 2d index supports $center.
$geometry Specifies a geometry in GeoJSON format to geospatial query operators.
$maxDistance Specifies a maximum distance to limit the results of $near and $nearSphere queries. The 2dsphere and 2d indexes support $centerSphere.
$minDistance Specifies a minimum distance to limit the results of $near and $nearSphere queries. For use with 2dsphere index only.
$polygon Specifies a polygon to using legacy coordinate pairs for $geoWithin queries. The 2d index supports $center.
$uniqueDocs Deprecated. Modifies a $geoWithin and $near queries to ensure that even if a document matches the query multiple times, the query returns the document once.

Geospatial Query Compatibility

While numerous combinations of query operators are possible, the following table shows the recommended operators for different types of queries. The table uses the $geoWithin, $geoIntersects and $near operators.

Query Document Geometry of the Query Condition Surface Type for Query Calculation Units for Query Calculation Supported by this Index
Returns points, lines and polygons        
{ $geoWithin : {
  $geometry : <GeoJSON Polygon>
} }
polygon sphere meters 2dsphere
{ $geoIntersects : {
  $geometry : <GeoJSON>
} }
point, line or polygon sphere meters 2dsphere
{ $near : {
  $geometry : <GeoJSON Point>,
  $maxDistance : d
} }
point sphere meters

2dsphere

The index is required.

Returns points only        
{ $geoWithin : {
  $box : [[x1, y1], [x2, y2]]
} }
rectangle flat flat units 2d
{ $geoWithin : {
  $polygon : [[x1, y1],
              [x1, y2],
              [x2, y2],
              [x2, y1]]
} }
polygon flat flat units 2d
{ $geoWithin : {
  $center : [[x1, y1], r],
} }
circular region flat flat units 2d
{ $geoWithin : {
  $centerSphere :
    [[x, y], radius]
} }
circular region sphere radians

2d

2dsphere

{ $near : [x1, y1],
  $maxDistance : d
}
point flat / flat units flat units

2d

The index is required.

←   $where $geoIntersects  →