Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Der $polygon-Operator definiert ein Polygon für Geospatialabfragen, sodass Sie Standorte innerhalb einer unregelmäßigen Form finden können. Der Operator ist nützlich zum Abfragen von Standorten innerhalb komplexer geografischer Grenzen.
Syntax
{
<location field>: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[[<longitude1>, <latitude1>], ..., [<longitudeN>, <latitudeN>], [<longitude1>, <latitude1>]]
]
}
}
}
}
Die Parameter
| Parameter | Description |
|---|---|
location field |
Das Feld, das die Geospatialdaten enthält |
coordinates |
Ein Array von Koordinatenpaaren, die das Polygon bilden. Die ersten und letzten Punkte müssen identisch sein, um das Polygon zu schließen |
Examples
Lassen Sie uns die Verwendung anhand der JSON-Beispieldatei aus stores-Dataset verstehen.
{
"_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
}
Beispiel 1 : Suchen innerhalb eines Polygons
Die Abfrage ruft Speicher ab, die auf der Grundlage der bereitgestellten Koordinaten in einen benutzerdefinierten Polygonbereich fallen.
db.stores.find({
location: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [[
[-141.9922, 16.8331], // VanArsdel Picture Frame Store
[-112.7858, -29.1866], // First Up Consultants Microphone Bazaar
[-38.4071, -47.2548], // Fabrikam Car Accessory Outlet
[-141.9922, 16.8331] // Close the polygon by repeating first point
]]
}
}
}
},
{
name: 1,
location: 1
}).limit(2)
Die ersten beiden Ergebnisse, die von dieser Abfrage zurückgegeben werden, sind:
[
{
"_id": "4a417727-a002-4c80-a01f-bc9526b300a5",
"name": "Northwind Traders | Bed and Bath Deals - East Duane",
"location": {
"type": "Point",
"coordinates": [-46.1444, -60.9697]
}
},
{
"_id": "1e27040c-7242-4970-8893-e5738e1bc1ca",
"name": "Northwind Traders | Seasonal Decoration Bazaar - Cassidyberg",
"location": {
"type": "Point",
"coordinates": [-44.3617, -81.2186]
}
}
]