On this page本页内容
$geoNear
Outputs documents in order of nearest to farthest from a specified point.按距离指定点最近或最远的顺序输出文档。
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
阶段的limmit
和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
阶段具有以下原型形式:
{ $geoNear: { <geoNear options> } }
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:以与已处理文档坐标系相同的单位指定所有距离:
distanceField | string | |
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 | |
key |
| |
maxDistance | number |
|
minDistance | number |
|
near |
| |
query | document |
|
spherical | boolean |
|
uniqueDocs | boolean |
|
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
requires a geospatial index.需要地理空间索引。
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个文档的默认限制。near
parameter supports the let option and bound let option.near
参数支持let
选项和绑定let
选项。Starting in MongoDB 5.3, you can use the 从MongoDB 5.3开始,您可以使用$geoNear
pipeline operator with a:$geoNear
管道运算符配合:
Create a collection 使用以下文档创建集合places
with the following documents:places
:
db.places.insertMany( [ { name: "Central Park", location: { type: "Point", coordinates: [ -73.97, 40.77 ] }, category: "Parks" }, { name: "Sara D. Roosevelt Park", location: { type: "Point", coordinates: [ -73.9928, 40.7193 ] }, category: "Parks" }, { name: "Polo Grounds", location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] }, category: "Stadiums" } ] )
The following operation creates a 以下操作将在位置字段上创建2dsphere
index on the location
field:2dsphere
索引:
db.places.createIndex( { location: "2dsphere" } )
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 上面的places
collection above has a 2dsphere
index. places
集合有一个2dsphere
索引。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
的文档。
db.places.aggregate([ { $geoNear: { near: { type: "Point", coordinates: [ -73.99279 , 40.719296 ] }, distanceField: "dist.calculated", maxDistance: 2, query: { category: "Parks" }, includeLocs: "dist.location", spherical: true } } ])
The aggregation returns the following:聚合返回以下内容:
{ "_id" : 8, "name" : "Sara D. Roosevelt Park", "category" : "Parks", "location" : { "type" : "Point", "coordinates" : [ -73.9928, 40.7193 ] }, "dist" : { "calculated" : 0.9539931676365992, "location" : { "type" : "Point", "coordinates" : [ -73.9928, 40.7193 ] } } }
The matching document contains two new fields:匹配文档包含两个新字段:
dist.calculated
dist.location
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 with a location at least 2 meters from the center 以下聚合查找距离中心[ -73.99279 , 40.719296 ]
and category
equal to Parks
.[ -73.99279 , 40.719296 ]
至少2米并且category
等于Parks
的所有文档。
db.places.aggregate([ { $geoNear: { near: { type: "Point", coordinates: [ -73.99279 , 40.719296 ] }, distanceField: "dist.calculated", minDistance: 2, query: { category: "Parks" }, includeLocs: "dist.location", spherical: true } } ])
let
option$geoNear
和let
选项In this example:在这个例子中:
let
option is used to set an array value of [-73.99279,40.719296]
to the variable $pt
.let
选项用于将数组值[-73.99279,40.719296]
设置为变量$pt
。$pt
near
parameter in the $geoNear
stage.$geoNear
阶段中的near
参数的let
选项。db.places.aggregate( [ { "$geoNear": { "near":"$$pt", "distanceField":"distance", "maxDistance":2, "query":{"category":"Parks"}, "includeLocs":"dist.location", "spherical":true } } ], { "let":{ "pt": [ -73.99279, 40.719296 ] } } )
The aggregation returns all documents with:聚合返回包含以下内容的所有文档:
let
variablelet
变量中定义的点最多2米的位置category
equal to Parks
.Parks
的category
。{ _id: ObjectId("61715cf9b0c1d171bb498fd7"), name: 'Sara D. Roosevelt Park', location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] }, category: 'Parks', distance: 1.4957325341976439e-7, dist: { location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] } } }, { _id: ObjectId("61715cf9b0c1d171bb498fd6"), name: 'Central Park', location: { type: 'Point', coordinates: [ -73.97, 40.77 ] }, category: 'Parks', distance: 0.0009348548688841822, dist: { location: { type: 'Point', coordinates: [ -73.97, 40.77 ] } } }
let
Option$geoNear
带绑定let
选项The let
option can bind a variable which can be used in a $geoNear query.let
选项可以绑定可以在$geoNear
查询中使用的变量。
In this example, 在本例中,$lookup
uses:$lookup
使用:
let
$pt
.$pt
。$geoNear
in the pipeline
.pipeline
中的$geoNear
。$pt
to define near
in the $geoNear
pipeline stage.$geoNear
管道阶段定义near
的$pt
。db.places.aggregate( [ { $lookup: { from: "places", let: { pt: "$location" }, pipeline: [ { $geoNear: { near: "$$pt", distanceField: "distance" } } ], as: "joinedField" } }, { $match: { name: "Sara D. Roosevelt Park" } } ] );
The aggregation returns a document with:聚合返回包含以下内容的文档:
$pt
variable for calculating distance.places
集合中的每个文档都作为子文档使用$pt
变量计算距离。{ _id: ObjectId("61715cf9b0c1d171bb498fd7"), name: 'Sara D. Roosevelt Park', location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] }, category: 'Parks', joinedField: [ { _id: ObjectId("61715cf9b0c1d171bb498fd7"), name: 'Sara D. Roosevelt Park', location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] }, category: 'Parks', distance: 0 }, { _id: ObjectId("61715cf9b0c1d171bb498fd6"), name: 'Central Park', location: { type: 'Point', coordinates: [ -73.97, 40.77 ] }, category: 'Parks', distance: 5962.448255234964 }, { _id: ObjectId("61715cfab0c1d171bb498fd8"), name: 'Polo Grounds', location: { type: 'Point', coordinates: [ -73.9375, 40.8303 ] }, category: 'Stadiums', distance: 13206.535424939102 } ] }
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
集合中的文档类似于:
{ "_id" : 3, "name" : "Polo Grounds", "location": { "type" : "Point", "coordinates" : [ -73.9375, 40.8303 ] }, "legacy" : [ -73.9375, 40.8303 ], "category" : "Stadiums" }
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
字段值,而不是传统字段值。The pipeline also uses 管道还使用$limit
to return at most 5 documents.$limit
返回最多5个文档。
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
阶段一起使用。
db.places.aggregate([ { $geoNear: { near: { type: "Point", coordinates: [ -73.98142 , 40.71782 ] }, key: "location", distanceField: "dist.calculated", query: { "category": "Parks" } } }, { $limit: 5 } ])
The aggregation returns the following:聚合返回以下内容:
{ "_id" : 8, "name" : "Sara D. Roosevelt Park", "location" : { "type" : "Point", "coordinates" : [ -73.9928, 40.7193 ] }, "category" : "Parks", "dist" : { "calculated" : 974.175764916902 } } { "_id" : 1, "name" : "Central Park", "location" : { "type" : "Point", "coordinates" : [ -73.97, 40.77 ] }, "legacy" : [ -73.97, 40.77 ], "category" : "Parks", "dist" : { "calculated" : 5887.92792958097 } }