该 $maxDistance
运算符用于地理空间查询,以指定两个点之间可存在的最大距离(以米为单位)。 它与 $near
基于半径的位置搜索很好地配对。
{
<location field>: {
$near: {
$geometry: {
type: "Point",
coordinates: [<longitude>, <latitude>]
},
$maxDistance: <distance in meters>
}
}
}
参数 | DESCRIPTION |
---|---|
location field |
包含地理空间数据的字段 |
coordinates |
指定中心点的 [经度, 纬度] 数组 |
$maxDistance |
距离中心点的最大距离(以米为单位) |
stores
使用集合,让我们在 Point 坐标的 10Km 内查找所有存储。
db.stores.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [-77.9951,-62.7339]
},
$maxDistance: 10000 // 10 kilometers in meters
}
}
},
{
name: 1,
location: 1
}).limit(2)
查询仅返回一个存储在提供的坐标的 10Km 以内的存储区。
{
"_id": "66fd4cdd-ffc3-44b6-81d9-6d5e9c1f7f9a",
"name": "Trey Research | Health Food Center - North Michelle",
"location": { "lat": -77.9951, "lon": -62.7339 }
}