On this page本页内容
$geoNear
¶Outputs documents in order of nearest to farthest from a specified point.按距离指定点最近到最远的顺序输出文档。
Note
Starting in version 4.2, MongoDB removes the 从4.2版开始,MongoDB删除了limit
and num
options for the $geoNear
stage as well as the default limit of 100 documents. $geoNear
阶段的limit
和num
选项,以及100个文档的默认限制。To limit the results of 要限制$geoNear
, use the $geoNear
stage with the $limit
stage.$geoNear
的结果,请将$geoNear
阶段与$limit
阶段一起使用。
The $geoNear
stage has the following prototype form:$geoNear
阶段的原型形式如下:
The $geoNear
operator accepts a document that contains the following $geoNear
options. $geoNear
运算符接受包含以下$geoNear
选项的文档。Specify all distances in the same units as those of the processed documents’ coordinate system:以与已处理文档坐标系相同的单位指定所有距离:
near |
| |
distanceField |
string | |
spherical |
boolean |
|
maxDistance |
number |
|
query |
document |
|
distanceMultiplier |
number | distanceMultiplier to convert radians, as returned by a spherical query, to kilometers by multiplying by the radius of the Earth.distanceMultiplier 将球形查询返回的弧度乘以地球半径,转换为千米。 |
includeLocs |
string | |
uniqueDocs |
boolean |
|
minDistance |
number |
|
key |
|
When using 当使用$geoNear
, consider that:$geoNear
时,请考虑:
$geoNear
as the first stage of a pipeline.$geoNear
用作管道的第一阶段。distanceField
option. distanceField
字段选项。distanceField
option specifies the field that will contain the calculated distance.distanceField
选项指定将包含计算距离的字段。$geoNear
If you have more than one geospatial index on the collection, use the 如果集合上有多个地理空间索引,请使用keys
parameter to specify which field to use in the calculation. keys
参数指定要在计算中使用的字段。If you have only one geospatial index, 如果只有一个地理空间索引,$geoNear
implicitly uses the indexed field for the calculation.$geoNear
会隐式使用索引字段进行计算。
$near
predicate in the query
field of the $geoNear
stage.$geoNear
阶段的query
字段中指定$near
谓词。$geoNear
pipeline stage).$geoNear
管道阶段)。$geoNear
no longer has a default limit of 100 documents.$geoNear
不再有100个文档的默认限制。Note
Starting in version 4.2, MongoDB removes the 从4.2版开始,MongoDB删除了limit
and num
options for the $geoNear
stage as well as the default limit of 100 documents. $geoNear
阶段的limit
和num
选项,以及100个文档的默认限制。To limit the results of 要限制$geoNear
, use the $geoNear
stage with the $limit
stage.$geoNear
的结果,请将$geoNear
阶段与$limit
阶段一起使用。
Consider a collection 考虑具有places
that has a 2dsphere
index. 2dsphere
索引的集合places
。The following aggregation uses 下面的聚合使用$geoNear
to find documents with a location at most 2 meters from the center [ -73.99279 , 40.719296 ]
and category
equal to Parks
.$geoNear
查找距离中心[ -73.99279 , 40.719296 ]
最多2米且category
等于Parks
的文档。
The aggregation returns the following:聚合返回以下内容:
The matching document contains two new fields:匹配的文档包含两个新字段:
dist.calculated
field that contains the calculated distance, anddist.calculated
字段,以及dist.location
field that contains the location used in the calculation.dist.location
字段。Note
Starting in version 4.2, MongoDB removes the 从4.2版开始,MongoDB删除了limit
and num
options for the $geoNear
stage as well as the default limit of 100 documents. $geoNear
阶段的limit
和num
选项,以及100个文档的默认限制。To limit the results of 要限制$geoNear
, use the $geoNear
stage with the $limit
stage.$geoNear
的结果,请将$geoNear
阶段与$limit
阶段一起使用。
The following example uses the option 下面的示例使用minDistance
to specify the minimum distance from the center point that the documents can be. minDistance
选项指定文档与中心点之间的最小距离。The following aggregation finds all documents that下面的聚合将查找
New in version 4.0.版本4.0中的新功能。
Consider a 考虑places
collection that has a 2dsphere index on the location
field and a 2d index on the legacy
field.places
集合在location
字段上具有2dsphere
索引、在legacy
字段上具有2d
索引。
A document in the places
collection resembles the following:places
集合中的文档类似于以下内容:
The following example uses the 以下示例使用key
option to specify that the aggregation should use the location
field values for the $geoNear
operation rather than the legacy
field values. key
选项指定聚合应使用$geoNear
操作的location
字段值,而不是legacy
字段值。The pipeline also uses 管道还使用$limit
to return at most 5 documents.$limit
返回最多5个文档。
Note
Starting in version 4.2, MongoDB removes the 从4.2版开始,MongoDB删除了limit
and num
options for the $geoNear
stage as well as the default limit of 100 documents. $geoNear
阶段的limit
和num
选项,以及100个文档的默认限制。To limit the results of 要限制$geoNear
, use the $geoNear
stage with the $limit
stage.$geoNear
的结果,请将$geoNear
阶段与$limit
阶段一起使用。
The aggregation returns the following:聚合返回以下内容: