On this page
$minDistance
在本页面
Definition
$minDistance
- 2.6 版的新功能。
将地理空间$near或$nearSphere查询的结果过滤到至少距中心点指定距离的那些文档。
$minDistance仅可用于2dsphere索引。
如果$near或$nearSphere查询将中心点指定为GeoJSON point,则将距离指定为米中的非负数。
如果$nearSphere查询将中心点指定为传统坐标对,则将距离指定为* radians *中的非负数。如果查询将中心点指定为GeoJSON point,则$near只能使用2dsphere索引。
Examples
与$ near 一起使用
Important
如果指定纬度和经度坐标,请先列出 经度 ,然后列出 latitude :
有效的经度值在
-180
和180
之间(包括两端值)。有效的纬度值在
-90
和90
之间(包括两端值)。
考虑具有2dsphere
索引的集合places
。
以下示例返回距指定的 GeoJSON 点至少1000
米,最多5000
米的文档,其排序方式为从最近到最远:
db.places.find(
{
location:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
与$ nearSphere 一起使用
考虑一个集合places
,该集合_2 包含具有location
字段并且具有2dsphere索引的文档。
然后,下面的示例返回其location
距指定点至少1000
米,并且距指定点最多5000
米,其 Sequences 为从最近到最远:
db.places.find(
{
location: {
$nearSphere: {
$geometry: {
type : "Point",
coordinates : [ -73.9667, 40.78 ]
},
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
有关将中心点指定为旧坐标对的示例,请参见$nearSphere