Docs HomeMongoDB Manual

$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查询将中心点指定为传统坐标对,则将距离指定为以弧度为单位的非负数。$near can only use the 2dsphere index if the query specifies the center point as a GeoJSON point.如果查询将中心点指定为GeoJSON点,$near只能使用2dsphere索引。

Examples实例

Use with $near

Important

If specifying latitude and longitude coordinates, list the longitude first, and then latitude.如果指定纬度和经度坐标,请先列出经度,然后列出纬度

  • Valid longitude values are between -180 and 180, both inclusive.有效的经度值介于-180180之间(包括-180180)。
  • Valid latitude values are between -90 and 90, both inclusive.有效的纬度值介于-9090之间(包括-9090)。

Consider a collection places that has a 2dsphere index.考虑一个具有2dsphere索引的集合places

The following example returns documents that are at least 1000 meters from and at most 5000 meters from the specified GeoJSON point, sorted from nearest to farthest:以下示例返回距离指定GeoJSON点至少1000米、最多5000米的文档,按从最近到最远排序:

db.places.find(
{
location:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)

Use with $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