$geoIntersects 運算子會選取位置欄位與指定的 GeoJSON 物件交集的文件。 當您想要尋找與特定地理區域有交集的商店時,此運算子很有用。
語法
{
<location field>: {
$geoIntersects: {
$geometry: {
type: <GeoJSON type>,
coordinates: <coordinates>
}
}
}
}
參數
| 參數 | Description |
|---|---|
location field |
包含 GeoJSON 物件的欄位 |
type |
GeoJSON 物件類型 (例如 "Polygon"、"MultiPolygon") |
coordinates |
定義 GeoJSON 物件的座標 |
範例
請參考商店集合中的此範例檔。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
範例 1 - 尋找地理位置相交的商店
若要獲得更好的效能,請從建立必要的 2dsphere 索引開始。
db.stores.createIndex({ "location": "2dsphere" })
範例查詢會使用集合尋找 stores 與特定多邊形區域相交的存放區。 此多邊形包含來自我們資料集中的數個商店位置。
db.stores.find(
{
location: {
$geoIntersects: {
$geometry: {
type: "Polygon",
coordinates: [[
[-80.0, -75.0],
[-80.0, -70.0],
[-55.0, -70.0],
[-55.0, -75.0],
[-80.0, -75.0]
]]
}
}
}
},
{
name: 1,
location: 1
}
).limit(2)
查詢會傳回存放區,其位置與座標所定義的多邊形輪廓相交。
[
{
"_id": "6bba7117-d180-4584-b50c-a2f843e9c9ab",
"name": "Wide World Importers | Craft Supply Mart - Heaneybury",
"location": { "lat": -64.4843, "lon": -107.7003 },
"city": "Heaneybury"
},
{
"_id": "2fd37663-e0ff-41d0-9c5a-3aec86285daa",
"name": "Relecloud | Cleaning Supply Closet - Patiencehaven",
"location": { "lat": -70.6077, "lon": -105.9901 },
"city": "Patiencehaven"
}
]
$geointersects運算子適用於下列案例:
- 尋找特定地理界限內的商店
- 識別服務涵蓋範圍區域
- 規劃傳遞路由
相關內容
- 檢視從 MongoDB 遷移到 Azure DocumentDB 的選項。
- 閱讀有關 與 MongoDB 的功能相容性的更多資訊。