On this page本页内容
$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.$near运算符可以指定GeoJSON点或传统坐标点。
$near requires a geospatial index:需要地理空间索引:
2d索引(如果使用传统坐标指定点)。To specify a GeoJSON point, 要指定GeoJSON点,$near operator requires a 2dsphere index and has the following syntax:$near运算符需要2dsphere索引,并具有以下语法:
{
<location field>: {
$near: {
$geometry: {
type: "Point" ,
coordinates: [ <longitude> , <latitude> ]
},
$maxDistance: <distance in meters>,
$minDistance: <distance in meters>
}
}
}
If specifying latitude and longitude coordinates, list the longitude first and then latitude:如果指定纬度和经度坐标,请先列出经度,然后列出纬度:
-180 and 180, both inclusive.-180和180之间(包括两者)。-90 and 90, both inclusive.-90和90之间,包括-90和90。When specifying a GeoJSON point, you can use the optional指定GeoJSON点时,可以使用可选的$minDistance and $maxDistance specifications to limit the $near results by distance in meters:$minDistance和$maxDistance规范,以米为单位限制$near结果:
$minDistance$maxDistanceTo specify a point using legacy coordinates, 要使用传统坐标指定点,$near requires a 2d index and has the following syntax:$near需要2d索引,并具有以下语法:
{
$near: [ <x>, <y> ],
$maxDistance: <distance in radians>
}
When specifying a legacy coordinate, you can use the optional指定传统坐标时,可以使用可选的$maxDistance specification to limit the $near results by distance in radians. $maxDistance规范按距离(弧度)限制$near结果。$maxDistance limits the results to those documents that are at most the specified distance from the center point.将结果限制为最多距中心点指定距离的文档。
You cannot combine the 不能将需要特殊地理空间索引的$near operator, which requires a special geospatial index, with a query operator or command that requires another special index. $near运算符与需要其他特殊索引的查询运算符或命令组合使用。For example you cannot combine 例如,不能将$near with the $text query.$near与$text查询组合在一起。
Starting in MongoDB 4.0, 从MongoDB4.0开始,分片集合支持$near queries are supported for sharded collections.$near查询。
In earlier MongoDB versions, 在早期的MongoDB版本中,分片集合不支持$near queries are not supported for sharded collections; instead, for sharded clusters, you must use the $geoNear aggregation stage or the geoNear command (available in MongoDB 4.0 and earlier).$near查询;相反,对于分片集群,必须使用$geoNear聚合阶段或geoNearl命令(在MongoDB4.0和更早版本中可用)。
$near 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 $near. sort(),sort()会对匹配的文档重新排序,从而有效地覆盖$near已经执行的排序操作。When using 在对地理空间查询使用sort() with geospatial queries, consider using $geoWithin operator, which does not sort documents, instead of $near.sort()时,请考虑使用$geoWithin运算符,而不是使用$near,因为$geoWithin不会对文档进行排序。
If specifying latitude and longitude coordinates, list the longitude first and then latitude:如果指定纬度和经度坐标,请先列出经度,然后列出纬度:
-180 and 180, both inclusive.-180和180之间(包括两者)。-90 and 90, both inclusive.-90和90之间(包括两者)。Consider a collection 考虑一个具有places that has a 2dsphere index.2dsphere索引的集合places。
The following example returns documents that are at least 以下示例返回距离指定GeoJSON点至少1000 meters from and at most 5000 meters from the specified GeoJSON point, sorted from nearest to farthest:1000米、最多5000米的文档,从最近到最远排序:
db.places.find(
{
location:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
If specifying latitude and longitude coordinates, list the longitude first and then latitude:如果指定纬度和经度坐标,请先列出经度,然后列出纬度:
-180 and 180, both inclusive.-180和180之间(包括两者)。-90 and 90, both inclusive.-90和90之间(包括两者)。Consider a collection 考虑一个具有legacy2d that has a 2d index.2d索引的集合legacy2d。
The following example returns documents that are at most 以下示例返回的文档与指定的旧坐标对之间的弧度最多为0.10 radians from the specified legacy coordinate pair, sorted from nearest to farthest:0.10,从最近到最远排序:
db.legacy2d.find(
{ location : { $near : [ -73.9667, 40.78 ], $maxDistance: 0.10 } }
)