$centerSphere

연산자는 $centerSphere 쿼리에 구형 기하 도형을 사용하여 원을 $geoWithin 지정합니다. 이 연산자는 지구의 구형 모양을 고려해야 하는 지리적 계산에 유용합니다.

문법

{
  $geoWithin: {
    $centerSphere: [ [ <x>, <y> ], <radius in radians> ]
  }
}

매개 변수

매개 변수 Description
<x> 원 중심점의 경도
<y> 원 중심점의 위도
<radius in radians> 라디안 구의 반경(라디안으로 변환하려면 킬로미터 단위로 거리를 6371로 나눕니다).

예시

스토어 컬렉션에서 이 샘플 문서를 고려합니다.

{
    "_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: 원형 영역 내의 저장소 찾기(지구의 구형 모양에 대한 계산)

예제 쿼리는 Wide World Importers Headphone Corner 매장 위치의 약 1,000km(반경 ≈ 0.157 라디안) 내에 있는 두 개의 매장을 찾습니다. 이 쿼리는 지역 마케팅 캠페인 또는 공급망 최적화를 위해 인근 매장을 식별하는 데 도움이 될 수 있습니다.

// Convert 1000km to radians: 1000/6371 ≈ 0.157
db.stores.find(
  {
    location: {
      $geoWithin: {
        $centerSphere: [[-82.5543, -65.105], 0.157]
      }
    }
  },
  {
    _id: 0,
    name: 1,
    location: 1,
    city: 1
  }
).limit(2)

이 쿼리에서 반환된 처음 두 가지 결과는 다음과 같습니다.

[
  {
    "name": "Fourth Coffee | Electronics Bazaar - O'Keefeburgh",
    "location": { "lat": -64.5856, "lon": -115.5241 },
    "city": "O'Keefeburgh"
  },
  {
    "name": "Boulder Innovations | Footwear Outlet - West Sybleberg",
    "location": { "lat": -72.73, "lon": -60.2306 },
    "city": "West Sybleberg"
  }
]

중요합니다

연산자는 $centerSphere 구형 기하 도형을 사용하여 거리를 계산하므로 지구 기반 계산에 대해 보다 $center정확합니다.

반지름은 라디안으로 지정해야 합니다.

좌표는 [경도, 위도] 순서로 지정해야 합니다.

지리적 버퍼가 UTM 영역을 넘어 확장되거나 국제 날짜선을 넘으면 결과가 정확하지 않거나 예측할 수 없을 수 있습니다.