$minDistance
On this page本页内容
Definition定义
$minDistance-
Filters the results of a geospatial将地理空间$nearor$nearSpherequery to those documents that are at least the specified distance from the center point.$near或$nearSphere查询的结果筛选为距离中心点至少指定距离的文档。If如果$nearor$nearSpherequery specifies the center point as a GeoJSON point, specify the distance as a non-negative number in meters.$near或$nearSphere查询将中心点指定为GeoJSON点,则将距离指定为以米为单位的非负数。If如果$nearSpherequery specifies the center point as legacy coordinate pair, specify the distance as a non-negative number in radians.$nearSphere查询将中心点指定为传统坐标对,则将距离指定为以弧度为单位的非负数。如果查询将中心点指定为GeoJSON点,$nearcan 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有效的经度值介于-180and180, both inclusive.-180和180之间(包括-180和180)。Valid latitude values are between有效的纬度值介于-90and90, 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一起使用
$nearSphereConsider 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