$center

$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 データセットを使用して、半径 50 度の "First Up Consultants Microphone Bazaar" 内にあるすべての店舗を見つけましょう。 このクエリは、First Up Consultants Microphone Bazaar の場所から半径 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"
  }
]

Important

$center 演算子は、フラットなユークリッド平面で動作します。

より正確な地球のような球面計算を行うには、代わりに $centerSphere を使用します。

半径は、使用されている座標系と同じ単位で指定されます。