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选项指定将包含计算距离的字段。$geoNearIf 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:聚合返回以下内容: