This version of the documentation is archived and no longer supported.


On this page



Specifies a point for which a geospatial query returns the closest documents first. The query returns the documents from nearest to farthest. The geoNear command provides an alternative to the $near operator. In addition to the functionality of $near, geoNear returns additional diagnostic information.

The geoNear command can use either a GeoJSON point or legacy coordinate pairs. Queries that use a 2d index return a limit of 100 documents.

The geoNear command requires that a collection have at most only one 2d index and/or only one 2dsphere.

The geoNear command accepts a document that contains the following fields. Specify all distances in the same units as the document coordinate system:

Field Type Description
geoNear string The collection to query.
near GeoJSON point or legacy coordinate pairs The point for which to find the closest documents.
limit number Optional. The maximum number of documents to return. The default value is 100. See also the num option.
num number Optional. The num option provides the same function as the limit option. Both define the maximum number of documents to return. If both options are included, the num value overrides the limit value.
maxDistance number 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.
query document Optional. Limits the results to the documents that match the query. The query syntax is the usual MongoDB read operation query syntax.
spherical Boolean

Required if using a 2dsphere index. For use with 2dsphere indexes, spherical must be true.

The default value is false.

distanceMultiplier number Optional. The factor to multiply all distances returned by the query. For example, use the distanceMultiplier to convert radians, as returned by a spherical query, to kilometers by multiplying by the radius of the Earth.
includeLocs 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 a subdocument, use dot notation.
uniqueDocs Boolean 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 is false, the query returns a document multiple times if the document has multiple matching location fields. See $uniqueDocs for more information.

Command Format

2dsphere Index

To query a 2dsphere index, use the following syntax:

db.runCommand( { geoNear : <collection> ,
                 near : { type : "Point" ,
                          coordinates: [ <coordinates> ] },
                 spherical : true } )

You must include spherical: true option.

2d Index

To query a 2d index, use:

{ geoNear : <collection> , near : [ <coordinates> ] }