MongoDB Query Language (MQL) Geospatial Operators

$box

Defines a rectangular area for geospatial queries using two coordinate pairs, useful for finding locations within a rectangular geographical boundary.

$center

Specifies a circle using legacy coordinate pairs for $geoWithin queries, defining a circle for geospatial query on a flat, Euclidean plane.

$centerSphere

Specifies a circle using spherical geometry for $geoWithin queries, more accurate for Earth-based calculations that account for spherical shape.

$geoIntersects

Selects documents whose location field intersects with a specified GeoJSON object, useful for finding locations that intersect with specific geographical areas.

$geometry

Specifies a GeoJSON geometry object for geospatial queries, used within other geospatial operators to define shapes and points for spatial calculations.

$geoWithin

Selects documents whose location field falls completely within a specified geometry, supporting various shape operators including $box, $polygon, $center, and $geometry.

$maxDistance

Specifies the maximum distance (in meters) that can exist between two points in a geospatial query, typically used with $near for radius-based location searches.

$minDistance

Specifies the minimum distance (in meters) that must exist between two points in a geospatial query, useful for finding locations outside a certain radius.

$near

Returns documents whose location field is near a specified point, sorted by distance, requiring a 2dsphere index and returning documents from nearest to farthest.

$nearSphere

Returns documents with location fields near a specified point on a sphere, calculating distances using spherical geometry, more accurate for Earth-based calculations than $near.

$polygon

Defines a polygon for geospatial queries, allowing you to find locations within an irregular shape, useful for querying locations within complex geographical boundaries.