- Reference >
- Query, Update, Projection, and Aggregation Operators >
- $maxDistance
$maxDistance¶
-
$maxDistance¶ The
$maxDistanceoperator specifies an upper bound to limit the results of a geolocation query. See below, where the$maxDistanceoperator narrows the results of the$nearquery:This query will return documents with
locationfields fromcollectionthat have values with a distance of 5 or fewer units from the point[100,100].$nearreturns results ordered by their distance from[100,100]. This operation will return the first 100 results unless you modify the query with thecursor.limit()method.Specify the value of the
$maxDistanceargument in the same units as the document coordinate system.Changed in version 2.2.3: Before 2.2.3, a geospatial index must exist on a field holding coordinates before using any of the geolocation query operators. After 2.2.3, applications may use geolocation query operators without having a geospatial index; however, geospatial indexes will support much faster geospatial queries than the unindexed equivalents.
Note
A geospatial index must exist on a field and the field must hold coordinates before you can use any of the geolocation query operators.