OPTIONS
翻译或纠错本页面

$near

Definition

$near

Specifies a point for which a geospatial query returns the documents from nearest to farthest. The $near operator can specify either a GeoJSON point or legacy coordinate point.

To specify a GeoJSON point, $near operator requires a 2dsphere index and has the following syntax:

{
  $near: {
     $geometry: {
        type: "Point" ,
        coordinates: [ <longitude> , <latitude> ]
     },
     $maxDistance: <distance in meters>,
     $minDistance: <distance in meters>
  }
}

When specifying a GeoJSON point, you can use the optional $minDistance and $maxDistance specifications to limit the $near results by distance in meters:

  • $minDistance limits the results to those documents that are at least the specified distance from the center point. $minDistance is only available for use with 2dsphere index.

    2.6 新版功能.

  • $maxDistance limits the results to those documents that are at most the specified distance from the center point.

To specify a point using legacy coordinates, $near requires a 2d index and has the following syntax:

{
  $near: [ <x>, <y> ],
  $maxDistance: <distance in radians>
}

If you use longitude and latitude for legacy coordinates, specify the longitude first, then latitude.

When specifying a legacy coordinate, you can use the optional $maxDistance specification to limit the $near results by distance in radians. $maxDistance limits the results to those documents that are at most the specified distance from the center point.

Considerations

  • $near queries that use a 2d index return a limit of 100 documents.
  • You cannot combine the $near operator, which requires a special geospatial index, with a query operator or command that uses a different type of special index. For example you cannot combine $near with the $text query.
  • If using a 2d index for $near, specifying a batch size (i.e. batchSize()) in conjunction with $near queries that use a 2d index is undefined. See SERVER-5236 for more information.
  • For sharded collections, queries using $near are not supported. You can instead use either the geoNear command or the $geoNear aggregation stage.
  • geoNear always returns the documents sorted by distance. Any other sort order requires to sort the documents in memory, which can be inefficient. To return results in a different sort order, use the $geoWithin operator and the sort() method.

Examples

Query on GeoJSON Data

重要

Specify coordinates in this order: “longitude, latitude.”

Consider a collection places that has a 2dsphere index.

The following example returns documents that are at least 1000 meters from and at most 5000 meters from the specified GeoJSON point, sorted from nearest to farthest:

db.places.find(
   {
     location:
       { $near :
          {
            $geometry: { type: "Point",  coordinates: [ -73.9667, 40.78 ] },
            $minDistance: 1000,
            $maxDistance: 5000
          }
       }
   }
)

Query on Legacy Coordinates

重要

Specify coordinates in this order: “longitude, latitude.”

Consider a collection legacy2d that has a 2d index.

The following example returns documents that are at most 0.10 radians from the specified legacy coordinate pair, sorted from nearest to farthest:

db.legacy2d.find(
   { location : { $near : [ -73.9667, 40.78 ], $maxDistance: 0.10 } }
)

The result set contains at most 100 documents.