=========== $nearSphere =========== .. default-domain:: mongodb .. contents:: On this page :local: :backlinks: none :depth: 1 :class: singlecol .. query:: $nearSphere .. versionadded:: 1.8 Specifies a point for which a :term:`geospatial` query returns the closest documents first. The query sorts the documents from nearest to farthest. MongoDB calculates distances for :query:`$nearSphere` using spherical geometry. The :query:`$nearSphere` operator queries for points defined by either :term:`GeoJSON` objects or legacy coordinate pairs. The optional :query:`$maxDistance` operator limits a :query:`$nearSphere` query to return only those documents that fall within a maximum distance of a point. If you use :query:`$maxDistance` on GeoJSON points, the distance is measured in meters. If you use :query:`$maxDistance` on legacy coordinate pairs, the distance is measured in radians. The :query:`$nearSphere` operator requires a geospatial index. The ``2dsphere`` and ``2d`` indexes both support :query:`$nearSphere` with both legacy coordinate pairs and GeoJSON points. Queries that use a ``2d`` index return at most 100 documents. .. important:: If you use longitude and latitude, specify **longitude first**. For queries on GeoJSON data, use the following syntax: .. code-block:: javascript db..find( { : { $nearSphere : { $geometry : { type : "Point" , coordinates : [ , ] } , $maxDistance : } } } ) For queries on legacy coordinate pairs, use the following syntax: .. code-block:: javascript db..find( { : { $nearSphere: [ , ] , $maxDistance: } } ) The following example selects the 100 documents with legacy coordinates pairs nearest to ``[ 40 , 5 ]``, as calculated by spherical geometry: .. code-block:: javascript db.places.find( { loc : { $nearSphere : [ 40 , 5 ] , $maxDistance : 10 } } )