- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Pipeline Aggregation Stages >
- $geoNear (aggregation)
$geoNear (aggregation)¶
On this page
Definition¶
-
$geoNear¶ Outputs documents in order of nearest to farthest from a specified point.
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 sphericalboolean Required if using a
2dsphereindex. Determines how MongoDB calculates the distance. The default value isfalse.If
true, then MongoDB uses spherical geometry to calculate distances in meters if the specified (near) point is a GeoJSON point and in radians if the specified (near) point is a legacy coordinate pair.If
false, then MongoDB uses 2d planar geometry to calculate distance between points.If using a 2dsphere index,
sphericalmust betrue.limitnumber Optional. The maximum number of documents to return. The default value is 100. See also thenumoption.numnumber Optional. The numoption provides the same function as thelimitoption. Both define the maximum number of documents to return. If both options are included, thenumvalue overrides thelimitvalue.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.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.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. 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. 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.
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.- The
$geoNearrequires that a collection have at most only one 2d index and/or only one 2dsphere index. - You do not need to specify which field in
the documents hold the coordinate pair or point. Because
$geoNearrequires that the collection have a single geospatial index,$geoNearimplicitly uses the indexed field. - If using a 2dsphere index, you must specify
spherical: true.
- You cannot specify a
$nearpredicate in thequeryfield of the$geoNearstage. - Views do not support geoNear operations (i.e.
geoNearcommand and$geoNearpipeline stage)
Generally, the options for $geoNear are similar to the
geoNear command with the following exceptions:
Example¶
Consider a collection places that has a 2dsphere index. The following
aggregation finds at most 5 unique documents with a location at
most 2 meters from the center [ -73.99279 , 40.719296 ] and have type equal
to public:
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¶
New in version 3.2.
The following example uses the option minDistance to specify
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.