$nearSphere 運算子會傳回位置欄位接近球體上指定點的文件,使用球形幾何計算距離。 該運算子比 $near 可更精確用於地球計算。
語法
{
<location field>: {
$nearSphere: {
$geometry: {
type: "Point",
coordinates: [<longitude>, <latitude>]
},
$maxDistance: <distance in meters>,
$minDistance: <distance in meters>
}
}
}
參數
| 參數 | Description |
|---|---|
location field |
包含 GeoJSON Point 的欄位 |
$geometry |
指定中心點的 GeoJSON Point 物件 |
$maxDistance |
選擇性。 球形表面上的最大距離 (以公尺為單位) |
$minDistance |
選擇性。 球形表面上的最小距離 (以公尺為單位) |
範例
我們來了解 stores 資料集範例 json 的使用方式。
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
"location": { "lat": -74.0427, "lon": 160.8154 },
"staff": { "employeeCount": { "fullTime": 9, "partTime": 18 } },
"sales": {
"salesByCategory": [ { "categoryName": "Stockings", "totalSales": 25731 } ],
"revenue": 25731
},
"promotionEvents": [
{
"eventName": "Mega Savings Extravaganza",
"promotionalDates": {
"startDate": { "Year": 2023, "Month": 6, "Day": 29 },
"endDate": { "Year": 2023, "Month": 7, "Day": 7 }
},
"discounts": [
{ "categoryName": "Stockings", "discountPercentage": 16 },
{ "categoryName": "Tree Ornaments", "discountPercentage": 8 }
]
},
{
"eventName": "Incredible Discount Days",
"promotionalDates": {
"startDate": { "Year": 2023, "Month": 9, "Day": 27 },
"endDate": { "Year": 2023, "Month": 10, "Day": 4 }
},
"discounts": [
{ "categoryName": "Stockings", "discountPercentage": 11 },
{ "categoryName": "Holiday Cards", "discountPercentage": 9 }
]
},
{
"eventName": "Massive Deal Mania",
"promotionalDates": {
"startDate": { "Year": 2023, "Month": 12, "Day": 26 },
"endDate": { "Year": 2024, "Month": 1, "Day": 2 }
},
"discounts": [
{ "categoryName": "Gift Bags", "discountPercentage": 21 },
{ "categoryName": "Bows", "discountPercentage": 19 }
]
},
{
"eventName": "Super Saver Soiree",
"promotionalDates": {
"startDate": { "Year": 2024, "Month": 3, "Day": 25 },
"endDate": { "Year": 2024, "Month": 4, "Day": 1 }
},
"discounts": [
{ "categoryName": "Tree Ornaments", "discountPercentage": 15 },
{ "categoryName": "Stockings", "discountPercentage": 14 }
]
},
{
"eventName": "Fantastic Savings Fiesta",
"promotionalDates": {
"startDate": { "Year": 2024, "Month": 6, "Day": 23 },
"endDate": { "Year": 2024, "Month": 6, "Day": 30 }
},
"discounts": [
{ "categoryName": "Stockings", "discountPercentage": 24 },
{ "categoryName": "Gift Wrap", "discountPercentage": 16 }
]
},
{
"eventName": "Price Plunge Party",
"promotionalDates": {
"startDate": { "Year": 2024, "Month": 9, "Day": 21 },
"endDate": { "Year": 2024, "Month": 9, "Day": 28 }
},
"discounts": [
{ "categoryName": "Holiday Tableware", "discountPercentage": 13 },
{ "categoryName": "Holiday Cards", "discountPercentage": 11 }
]
}
],
"company": "Lakeshore Retail",
"city": "Marvinfort",
"storeOpeningDate": { "$date": "2024-10-01T18:24:02.586Z" },
"lastUpdated": { "$timestamp": { "t": 1730485442, "i": 1 } },
"storeFeatures": 38
}
若要獲得更好的效能,請從建立必要的 2dsphere 索引開始。
db.stores.createIndex({ "location": "2dsphere" })
範例 1:基本球面搜尋
查詢會擷取球形 (類地球) 表面上最接近指定點 (-141.9922, 16.8331) 的存放區。
db.stores.find({
'location': {
$nearSphere: {
$geometry: {
type: "Point",
coordinates: [-141.9922, 16.8331]
}
}
}
}, {
name: 1,
location: 1
}).limit(2)
此查詢傳回的前兩個結果是:
[
{
"_id": "643b2756-c22d-4063-9777-0945b9926346",
"name": "Contoso, Ltd. | Outdoor Furniture Corner - Pagacfort",
"location": {
"type": "Point",
"coordinates": [152.1353, -89.8688]
}
},
{
"_id": "daa71e60-75d4-4e03-8b45-9df59af0811f",
"name": "First Up Consultants | Handbag Corner - South Salvatore",
"location": {
"type": "Point",
"coordinates": [150.2305, -89.8431]
}
}
]
範例 2:複數距離分析
此查詢會擷取距離點 20 公尺到 200 公尺之間的儲存 (65.3765, -44.8674)。 查詢會在「甜甜圈形狀」區域中搜尋 - 尋找距離指定點至少 20 公尺但不超過 200 公尺的商店。
db.stores.aggregate([
{
$geoNear: {
near: {
type: "Point",
coordinates: [65.3765, -44.8674]
},
distanceField: "sphericalDistance",
minDistance: 20,
maxDistance: 200,
spherical: true
}
},
{
$project: {
name: 1,
location: 1,
distanceKm: { $divide: ["$sphericalDistance", 1000] },
_id: 0
}
},
{
$limit: 2
}
])
運算子 $nearSphere 與 $near之間的主要差異。
- 舊版使用球形幾何進行距離計算。
- 前者對地球距離計算更準確。
- 前者更適合需要精確全域距離計算的應用程式