Returns documents in order of proximity to a specified point, from the nearest to farthest.
geoNearrequires a geospatial index.
Field Type Description
string The collection to query.
GeoJSON 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.
Required if using a
2dsphereindex. Determines how MongoDB calculates the distance. The default value is
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.
false, then MongoDB uses 2d planar geometry to calculate distance between points.
If using a 2dsphere index,
number Optional. The maximum number of documents to return. The default value is
100. See also the
number Optional. The
numoption provides the same function as the
limitoption. Both define the maximum number of documents to return. If both options are included, the
numvalue overrides the
Optional. The minimum distance from the center point that the documents must be. MongoDB filters the results to those documents that are at least the specified distance from the center point.
Only available for use with 2dsphere index.
New in version 2.6.
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.
Optional. Limits the results to the documents that match the query. The query syntax is the usual MongoDB read operation query syntax.
number 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.
boolean Optional. If this is
true, the query returns the location of the matching documents in the results. The default is
false. This option is useful when a location field contains multiple locations. To specify a field within an embedded document, use dot notation.
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.
Optional. Specifies the read concern.
The readConcern option has the following syntax:
Changed in version 3.6.
Possible read concern levels are:
"local". This is the default read concern level.
"available". This is the default for reads against secondaries when Read Operations and Causally Consistent Sessions and “level” are unspecified. The query returns the instance’s most recent data.
"majority". Available for replica sets that use WiredTiger storage engine.
"linearizable". Available for read operations on the
For more formation on the read concern levels, see Read Concern Levels.
"majority"read concern level, you can specify the
afterClusterTimeoption to have the read operation return data that meets the level requirement and the specified after cluster time requirement. For more information, see Read Operations and Causally Consistent Sessions.
If using a
2dsphere index, you can specify either a
GeoJSON point or a legacy coordinate pair for the
You must include
spherical: true in the syntax.
spherical: true, if you specify a GeoJSON point, MongoDB uses meters as the unit of measurement:
spherical: true, if you specify a legacy coordinate pair, MongoDB uses radians as the unit of measurement:
To query a 2d index, use the following syntax:
If you specify
spherical: true, MongoDB uses spherical geometry to calculate distances in radians. Otherwise, MongoDB uses planar geometry to calculate distances between points.
geoNear sorts documents by distance. If you also include a
sort() for the query,
sort() re-orders the matching documents, effectively overriding the sort operation already performed by
geoNear. When using
sort() with geospatial queries, consider using
$geoWithin operator, which does not sort documents, instead of
geoNear orders the documents from nearest to farthest, the
minDistance field effectively skips over the first n documents where n is determined by the distance requirement.
The following examples run the
geoNear command on the collection
places that has a
geoNear command queries for documents whose
"public" and returns the matching documents in order of nearest to farthest to the specified point:
The operation returns the following output, the documents in the
results from nearest to farthest: