- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $geoNear (aggregation)
$geoNear (aggregation)¶
On this page
Definition¶
-
$geoNear¶ Outputs documents in order of nearest to farthest from a specified point.
Note
Starting in version 4.2, MongoDB removes the
limitandnumoptions for the$geoNearstage as well as the default limit of 100 documents. To limit the results of$geoNear, use the$geoNearstage with the$limitstage.The
$geoNearstage has the following prototype form:The
$geoNearoperator accepts a document that contains the following$geoNearoptions. Specify all distances in the same units as those of the processed documents’ coordinate system:Field Type Description nearGeoJSON point or legacy coordinate pair The point for which to find the closest documents.
If using a 2dsphere index, you can specify the point as either a GeoJSON point or legacy coordinate pair.
If using a 2d index, specify the point as a legacy coordinate pair.
distanceFieldstring The output field that contains the calculated distance. To specify a field within an embedded document, use dot notation. sphericalboolean Optional. Determines how MongoDB calculates the distance between two points:
- When
true, MongoDB uses$nearSpheresemantics and calculates distances using spherical geometry. - When
false, MongoDB uses$nearsemantics: spherical geometry for 2dsphere indexes and planar geometry for 2d indexes.
Default: false.
maxDistancenumber Optional. The maximum distance from the center point that the documents can be. MongoDB limits the results to those documents that fall within the specified distance from the center point.
Specify the distance in meters if the specified point is GeoJSON and in radians if the specified point is legacy coordinate pairs.
querydocument Optional. Limits the results to the documents that match the query. The query syntax is the usual MongoDB read operation query syntax.
You cannot specify a
$nearpredicate in thequeryfield of the$geoNearstage.distanceMultipliernumber Optional. The factor to multiply all distances returned by the query. For example, use the distanceMultiplierto convert radians, as returned by a spherical query, to kilometers by multiplying by the radius of the Earth.includeLocsstring Optional. This specifies the output field that identifies the location used to calculate the distance. This option is useful when a location field contains multiple locations. To specify a field within an embedded document, use dot notation. uniqueDocsboolean Optional. If this value is
true, the query returns a matching document once, even if more than one of the document’s location fields match the query.Deprecated since version 2.6: Geospatial queries no longer return duplicate results. The
$uniqueDocsoperator has no impact on results.minDistancenumber Optional. The minimum distance from the center point that the documents can be. MongoDB limits the results to those documents that fall outside the specified distance from the center point.
Specify the distance in meters for GeoJSON data and in radians for legacy coordinate pairs.
New in version 3.2.
keyOptional. Specify the geospatial indexed field to use when calculating the distance.
If your collection has multiple
2dand/or multiple2dsphereindexes, you must use thekeyoption to specify the indexed field path to use. Specify Which Geospatial Index to Use provides a full example.If there is more than one
2dindex or more than one2dsphereindex and you do not specify akey, MongoDB will return an error.If you do not specify the
key, and you have at most only one2dindex and/or only one2dsphereindex, MongoDB looks first for a2dindex to use. If a2dindex does not exists, then MongoDB looks for a2dsphereindex to use.New in version 4.0.
- When
Behavior¶
When using $geoNear, consider that:
You can only use
$geoNearas the first stage of a pipeline.You must include the
distanceFieldoption. ThedistanceFieldoption specifies the field that will contain the calculated distance.$geoNearrequires a geospatial index.If you have more than one geospatial index on the collection, use the
keysparameter to specify which field to use in the calculation. If you have only one geospatial index,$geoNearimplicitly uses the indexed field for the calculation.
Example¶
Note
Starting in version 4.2, MongoDB removes the limit and num
options for the $geoNear stage as well as the default
limit of 100 documents. To limit the results of
$geoNear, use the $geoNear stage with the
$limit stage.
Consider a collection places that has a 2dsphere index. The
following aggregation uses $geoNear to find documents with
a location at most 2 meters from the center [ -73.99279 , 40.719296
] and category equal to Parks.
The aggregation returns the following:
The matching document contains two new fields:
dist.calculatedfield that contains the calculated distance, anddist.locationfield that contains the location used in the calculation.
Minimum Distance¶
Note
Starting in version 4.2, MongoDB removes the limit and num
options for the $geoNear stage as well as the default
limit of 100 documents. To limit the results of
$geoNear, use the $geoNear stage with the
$limit stage.
The following example uses the option minDistance to specify
the minimum distance from the center point that the documents can be.
The following aggregation finds all documents that
Specify Which Geospatial Index to Use¶
New in version 4.0.
Consider a places collection that has a 2dsphere index on the location field and a
2d index on the legacy field.
A document in the places collection resembles the following:
The following example uses the key option to specify that the
aggregation should use the location field values for the
$geoNear operation rather than the legacy field values.
The pipeline also uses $limit to return at most 5 documents.
Note
Starting in version 4.2, MongoDB removes the limit and num
options for the $geoNear stage as well as the default
limit of 100 documents. To limit the results of
$geoNear, use the $geoNear stage with the
$limit stage.
The aggregation returns the following: