$minDistance
On this page本页内容
Definition定义
$minDistance
-
Filters the results of a geospatial将地理空间$near
or$nearSphere
query to those documents that are at least the specified distance from the center point.$near
或$nearSphere
查询的结果筛选为距离中心点至少指定距离的文档。If如果$near
or$nearSphere
query specifies the center point as a GeoJSON point, specify the distance as a non-negative number in meters.$near
或$nearSphere
查询将中心点指定为GeoJSON点,则将距离指定为以米为单位的非负数。If如果$nearSphere
query specifies the center point as legacy coordinate pair, specify the distance as a non-negative number in radians.$nearSphere
查询将中心点指定为传统坐标对,则将距离指定为以弧度为单位的非负数。如果查询将中心点指定为GeoJSON点,$near
can only use the 2dsphere index if the query specifies the center point as a GeoJSON point.$near
只能使用2dsphere
索引。
Examples实例
Use with $near
If specifying latitude and longitude coordinates, list the longitude first, and then latitude.如果指定纬度和经度坐标,请先列出经度,然后列出纬度。
Valid longitude values are between有效的经度值介于-180
and180
, both inclusive.-180
和180
之间(包括-180
和180
)。Valid latitude values are between有效的纬度值介于-90
and90
, both inclusive.-90
和90
之间(包括-90
和90
)。
Consider a collection 考虑一个具有places
that has a 2dsphere
index.2dsphere
索引的集合places
。
The following example returns documents that are at least 以下示例返回距离指定GeoJSON点至少1000
meters from and at most 5000
meters from the specified GeoJSON point, sorted from nearest to farthest:1000
米、最多5000
米的文档,按从最近到最远排序:
db.places.find(
{
location:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
Use with $nearSphere
与$nearSphere
一起使用
$nearSphere
Consider a collection 考虑一个集合places
that contains documents with a location
field and has a 2dsphere index.places
,其中包含带有location
字段的文档,并具有2dsphere
索引。
Then, the following example returns whose 然后,以下示例按从最近到最远的顺序返回其位置,该location
is at least 1000
meters from and at most 5000
meters from the specified point, ordered from nearest to farthest:location
距离指定点至少1000
米,最多5000
米:
db.places.find(
{
location: {
$nearSphere: {
$geometry: {
type : "Point",
coordinates : [ -73.9667, 40.78 ]
},
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
For an example that specifies the center point as legacy coordinate pair, see 有关将中心点指定为传统坐标对的示例,请参阅$nearSphere
$nearSphere