定义
$geoNear按距离指定点最近到最远的顺序输出文档。
$geoNear阶段具有以下原型形式:{ $geoNear: { <geoNear options> } } $geoNear运算符接受包含以下$geoNear选项的文档。使用与已处理文档坐标系相同的单位指定所有距离:字段类型说明distanceField字符串
包含计算出的距离的输出字段。要在嵌入式文档中指定字段,请使用点符号。
distanceMultiplier数字
可选。用于与查询返回的所有距离相乘的系数。例如,使用
distanceMultiplier乘以地球半径,将球形查询返回的弧度转换为公里。includeLocs字符串
可选。这指定了一个输出字段,用于标识用来计算距离的位置。该选项适合位置字段包含多个位置的情况。要在嵌入式文档中指定字段,请使用点符号。
keymaxDistance数字
minDistance数字
可选。文档可与中心点相距的最小距离。MongoDB 会将结果限制为相距中心点指定距离之外的文档。
对于 GeoJSON 数据,以米为单位指定距离;对于传统坐标对,以弧度为单位指定距离。
版本 3.2 中的新增功能。
nearGeoJSON 点或传统坐标对
query文档
spherical布尔
行为
距离计算
$geoNear 根据输入文档周边的最近点计算距离。
示例,如果输入文档是一个形状,$geoNear 则会标识形状周长上距离指定点最近的点,并输出指定点与形状最近点之间的距离。
Considerations
在使用 $geoNear 时,请考虑:
示例
使用以下文档创建集合 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" } ] )
下面的操作将在 location 字段上创建一个 2dsphere 索引:
db.places.createIndex( { location: "2dsphere" } )
最大距离
注意
上面的 places 集合有一个 2dsphere 索引。以下聚合使用 $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 } } ])
该聚合返回以下内容:
{ "_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 ] } } }
匹配文档包含两个新字段:
dist.calculated字段,其包含计算出的距离,以及dist.location字段,包含计算中使用的位置。
最小距离
注意
以下示例使用选项 minDistance 来指定文档可与中心点相距的最小距离。以下聚合查找距离中心 [ -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 } } ])
指定要使用的地理空间索引
考虑 places 集合,它在 location 字段上有 2dsphere 索引,在 legacy 字段上有 2d 索引。
places 集合中的文档类似于以下内容:
{ "_id" : 3, "name" : "Polo Grounds", "location": { "type" : "Point", "coordinates" : [ -73.9375, 40.8303 ] }, "legacy" : [ -73.9375, 40.8303 ], "category" : "Stadiums" }
以下示例使用了 key 选项,具体指定聚合应使用 location 字段值来执行 $geoNear 操作,而不是使用 legacy 字段值。该管道还使用了 $limit,最多返回 5 个文档。
注意
db.places.aggregate([ { $geoNear: { near: { type: "Point", coordinates: [ -73.98142 , 40.71782 ] }, key: "location", distanceField: "dist.calculated", query: { "category": "Parks" } } }, { $limit: 5 } ])
该聚合返回以下内容:
{ "_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 } }