- Reference >
- Database Commands >
- Geospatial Commands >
- geoNear
geoNear¶
On this page
Definition¶
-
geoNear¶ Specifies a point for which a geospatial query returns the closest documents first. The query returns the documents from nearest to farthest. The
geoNearcommand provides an alternative to the$nearoperator. In addition to the functionality of$near,geoNearreturns additional diagnostic information.The
geoNearcommand can use either a GeoJSON point or legacy coordinate pairs. Queries that use a2dindex return a limit of 100 documents.The
geoNearcommand requires that a collection have at most only one 2d index and/or only one 2dsphere.The
geoNearcommand accepts a document that contains the following fields. Specify all distances in the same units as the document coordinate system:Field Type Description geoNearstring The collection to query. nearGeoJSON point or legacy coordinate pairs The point for which to find the closest documents. 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. A distance from the center point. Specify the distance in meters for GeoJSON data and in radians for legacy coordinate pairs. MongoDB limits the results to those documents that fall within the specified distance from the center point. querydocument Optional. Limits the results to the documents that match the query. The query syntax is the usual MongoDB read operation query syntax. sphericalBoolean Required if using a 2dsphere index. For use with
2dsphereindexes,sphericalmust betrue.The default value is
false.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.includeLocsBoolean Optional. If this is true, the query returns the location of the matching documents in the results. The default isfalse. This option is useful when a location field contains multiple locations. To specify a field within a subdocument, 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. If this value isfalse, the query returns a document multiple times if the document has multiple matching location fields. See$uniqueDocsfor more information.