$geoIntersects演算子は、場所フィールドが指定された GeoJSON オブジェクトと交差するドキュメントを選択します。 この演算子は、特定の地理的領域と交差する店舗を検索する場合に便利です。
構文
{
<location field>: {
$geoIntersects: {
$geometry: {
type: <GeoJSON type>,
coordinates: <coordinates>
}
}
}
}
パラメーター
| パラメーター | Description |
|---|---|
location field |
GeoJSON オブジェクトを含むフィールド |
type |
GeoJSON オブジェクトの種類 (例: "Polygon"、"MultiPolygon") |
coordinates |
GeoJSON オブジェクトを定義する座標 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_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)
このクエリは、座標によって定義された Polygon 輪郭と交差する位置を持つストアを返します。
[
{
"_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 との機能の互換性について詳しくは、こちらをご覧ください。