$maxDistance 연산자는 지리 공간적 쿼리에서 두 지점 사이에 존재할 수 있는 최대 거리(미터)를 지정하는 데 사용됩니다. 반경 기반의 위치 검색을 할 때 $near와 함께 사용할 수 있습니다.
문법
{
<location field>: {
$near: {
$geometry: {
type: "Point",
coordinates: [<longitude>, <latitude>]
},
$maxDistance: <distance in meters>
}
}
}
매개 변수
| 매개 변수 | Description |
|---|---|
location field |
지리 공간적 데이터를 포함하는 필드 |
coordinates |
중심점을 지정하는 [경도, 위도]의 배열입니다. |
$maxDistance |
중심점으로부터의 최대 거리(미터) |
예시
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: 특정 위치 근처에 있는 저장소 찾기
쿼리는 지정된 점 좌표 [-77.9951, -62.7339]의 반경 10km 내에 있는 최대 두 개의 저장소를 검색합니다.
db.stores.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [-77.9951,-62.7339]
},
$maxDistance: 10000 // 10 kilometers in meters
}
}
},
{
name: 1,
location: 1
}).limit(2)
쿼리는 다음 결과를 반환합니다.
[
{
"_id": "66fd4cdd-ffc3-44b6-81d9-6d5e9c1f7f9a",
"name": "Trey Research | Health Food Center - North Michelle",
"location": { "lat": -77.9951, "lon": -62.7339 }
}
]