$geometry

$geometry演算子は、地理空間クエリの GeoJSON ジオメトリ オブジェクトを指定します。 これは、他の地理空間演算子内で、空間計算の図形とポイントを定義するために使用されます。

構文

{
  $geometry: {
    type: <GeoJSON type>,
    coordinates: <coordinates>
  }
}

パラメーター

パラメーター Description
type GeoJSON オブジェクトの種類 (Point、Polygon、MultiPolygon など)
coordinates GeoJSON オブジェクトを配列として定義する座標

例示

stores データセットからのサンプル json の使用方法を理解しましょう。

{
    "_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" })

このクエリでは、座標 [46.2917, -62.6354] のポイントに最も近い最大 2 つのストアが近接順に取得されます。 $near演算子を使用して、特定のポイントからの距離で結果を並べ替え、特定の場所に地理的に最も近い店舗を見つけるのに役立ちます。

db.stores.find({
  location: {
    $near: {
      $geometry: {
        type: "Point",
        coordinates: [46.2917, -62.6354]
      }
    }
  }
}, {
  name: 1,
  location: 1
}).limit(2)

このクエリによって返される最初の 2 つの結果は次のとおりです。

[
  {
    "_id": "59c355e9-586c-44f8-bbaf-a87989142119",
    "name": "Relecloud | Outdoor Furniture Shop - Chetside",
    "location": { "lat": 46.188, "lon": -62.2789 }
  },
  {
    "_id": "d3a9cc23-e6ae-4806-93ac-1ade2f624742",
    "name": "VanArsdel, Ltd. | Furniture Place - North Dustinside",
    "location": { "lat": 47.3426, "lon": -62.4031 }
  }
]

例 2: ポリゴン ジオメトリに最も近いストアを検索する

このクエリは、[-80.0, -75.0] から [-55.0, -70.0] までの座標で囲まれた定義された四角形の多角形と交差する位置を持つ最大 2 つのストアを検索します。

$geoIntersects演算子は、ポリゴン境界と重なる、またはポリゴン境界に接するストアを検索します。これは、特定の地理的ゾーンと対話する場所を特定するのに最適です。その場所が完全に内側にあるか、エッジを越えているだけなのかを特定するのに最適です。

db.stores.find({
  location: {
    $geoIntersects: {
      $geometry: {
        type: "Polygon",
        coordinates: [[
          [-80.0, -75.0],   // Bottom-left
          [-80.0, -70.0],   // Top-left
          [-55.0, -70.0],   // Top-right
          [-55.0, -75.0],   // Bottom-right
          [-80.0, -75.0]    // Close polygon
        ]]
      }
    }
  }
}, {
  name: 1,
  location: 1,
  city: 1
}).limit(2)

このクエリによって返される最初の 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"
  }
]

例 3: マルチポリゴン ジオメトリに最も近いストアを検索する

この例では、2 つの定義された四角形領域 (MultiPolygon) のいずれかに位置する最大 2 つのストアを取得します。1 つは座標 [120.0, -13.0] から [125.0, -10.0]の近く、もう 1 つは [44.0, -64.0] から [48.0, -61.0] に近いストアです。

MultiPolygon ジオメトリを持つ $geoWithin 演算子を使用して、指定されたポリゴンで囲まれたストアを検索するため、隣接していない複数の地理的領域を同時に照会するのに役立ちます。

db.stores.find({
  location: {
    $geoWithin: {
      $geometry: {
        type: "MultiPolygon",
        coordinates: [
          [[
            [120.0, -13.0],
            [120.0, -10.0],
            [125.0, -10.0],
            [125.0, -13.0],
            [120.0, -13.0]
          ]],
          [[
            [44.0, -64.0],
            [44.0, -61.0],
            [48.0, -61.0],
            [48.0, -64.0],
            [44.0, -64.0]
          ]]
        ]
      }
    }
  }
}, {
  name: 1,
  location: 1
}).limit(2)

このクエリによって返される最初の 2 つの結果は次のとおりです。

[
  {
    "_id": "6d70de9c-7b83-426d-81aa-f2173f97b64d",
    "name": "Fabrikam, Inc. | Footwear Haven - Port Erling",
    "location": { "lat": 45.641, "lon": -118.4963 }
  },
  {
    "_id": "96d48224-ce10-4a61-999a-8536d442f81a",
    "name": "Wide World Importers | Eyewear Bazaar - West Oletachester",
    "location": { "lat": 47.3461, "lon": -61.6605 }
  }
]