$center 運算子會使用要用於 $geoWithin 查詢中的舊版座標組來指定圓形。 它會在平坦的歐幾里德平面上定義地理空間查詢的圓形。
語法
{
$geoWithin: {
$center: [ [ <x>, <y> ], <radius> ]
}
}
參數
| 參數 | Description |
|---|---|
<x> |
圓形中心點的 X 座標 |
<y> |
圓形中心點的 Y 座標 |
<radius> |
圓形的半徑與座標具有相同單位 |
範例
我們來了解 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
}
範例 1 - 尋找圓形區域內的商店
讓我們使用我們的 stores 資料集,找出 'First Up Consultants Microphone Bazaar' 50 度半徑內的所有商店。 此查詢會擷取 First Up Consultants 麥克風集市位置 50 度半徑內的商店。
db.stores.find(
{
location: {
$geoWithin: {
$center: [[-112.7858, -29.1866], 50]
}
}
},
{
name: 1,
city: 1,
location: 1,
_id: 0
}
).limit(2)
查詢會傳回 50 度半徑內的商店,這對於分析市場覆蓋範圍或規劃送貨路線很有用。
[
{
"name": "Contoso, Ltd. | Baby Products Corner - Port Jerrold",
"location": { "lat": -72.7709, "lon": -24.3031 },
"city": "Port Jerrold"
},
{
"name": "VanArsdel, Ltd. | Smart Home Closet - Trystanport",
"location": { "lat": -64.5509, "lon": -28.7144 },
"city": "Trystanport"
}
]
這很重要
$center 可在平坦的歐幾里德平面上運作。
如需更精確的類似地球的球形計算,請改用 $centerSphere。
半徑會以與要使用的座標系統相同的單位來指定。