Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
El $minDistance operador se usa en consultas geoespaciales para especificar la distancia mínima (en metros) que debe existir entre dos puntos. Resulta útil encontrar ubicaciones fuera de un radio determinado.
Syntax
{
<location field>: {
$near: {
$geometry: {
type: "Point",
coordinates: [<longitude>, <latitude>]
},
$minDistance: <distance in meters>
}
}
}
Parámetros
| Parámetro | Description |
|---|---|
location field |
Campo que contiene los datos geoespaciales |
coordinates |
Matriz de [longitud, latitud] que especifica el punto central |
$minDistance |
Distancia mínima en metros desde el punto central |
Examples
Vamos a comprender el uso con un JSON de ejemplo del conjunto de datos stores.
{
"_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
}
Ejemplo 1: Búsqueda de almacenes por distancia mínima desde un punto de referencia
La consulta de ejemplo permite buscar almacenes que están al menos a 500 kilómetros de la coordenada de punto [69.7296, 70.1272].
db.stores.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [69.7296, 70.1272] // Proseware Home Entertainment Hub location
},
$minDistance: 500000 // 500 kilometers in meters
}
}
},
{
name: 1,
location: 1
}).limit(2)
Los dos primeros resultados devueltos por esta consulta son:
[
{
"_id": "9d9d768b-4daf-4126-af15-a963bd3b88aa",
"name": "First Up Consultants | Perfume Gallery - New Verniceshire",
"location": { "lat": 36.0762, "lon": 98.7799 }
},
{
"_id": "76b03913-37e3-4779-b3b8-0f654c1ae3e7",
"name": "Fabrikam, Inc. | Turntable Depot - Schinnershire",
"location": { "lat": 37.5534, "lon": 81.6805 }
}
]