本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Amazon DocumentDB 數據查詢地理空間數據
本節介紹如何使用 Amazon DocumentDB 查詢地理空間資料。閱讀本節之後,您將能夠回答如何在 Amazon DocumentDB 中存儲,查詢和索引地理空間數據。
概觀
地理空間的常見使用案例涉及來自資料的接近分析。例如,「尋找西雅圖 50 英里範圍內的所有機場」,或「尋找指定位置最近的餐廳」。Amazon DocumentDB 使用地理JSON規範來表示地理
索引和存儲地理空間數據
Amazon DocumentDB 使用「點」地理JSON類型來存儲地理空間數據。每個 Geo JSON 文檔(或子文檔)通常由兩個字段組成:
-
類型-所表示的形狀,它通知 Amazon DocumentDB 如何解釋「坐標」字段。目前,Amazon DocumentDB 僅支持點
-
坐標-以數組中的對象表示的緯度和經度對-[經度,緯度]
Amazon DocumentDB 還使用 2dsphere 索引來索引地理空間數據。Amazon DocumentDB 支持索引點。Amazon DocumentDB 支持使用 2dsphere 索引進行近接查詢。
讓我們考慮一個場景,您正在構建食品外送服務的應用程序。你想存儲各種餐廳的緯度和經度對亞 Amazon DocumentDB。若要這麼做,首先建議您在「地理空間」欄位上建立索引,以保存緯度和經度對。
use restaurantsdb db.usarestaurants.createIndex({location:"2dsphere"})
這個命令的輸出看起來像這樣:
{ "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
一旦你已經創建了一個索引,你可以開始插入數據到你的 Amazon DocumentDB 集合。
db.usarestaurants.insert({ "state":"Washington", "city":"Seattle", "name":"Thai Palace", "rating": 4.8, "location":{ "type":"Point", "coordinates":[ -122.3264, 47.6009 ] } }); db.usarestaurants.insert({ "state":"Washington", "city":"Seattle", "name":"Noodle House", "rating": 4.8, "location":{ "type":"Point", "coordinates":[ -122.3517, 47.6159 ] } }); db.usarestaurants.insert({ "state":"Washington", "city":"Seattle", "name":"Curry House", "rating": 4.8, "location":{ "type":"Point", "coordinates":[ -121.4517, 47.6229 ] } });
查詢地理空間資料
Amazon DocumentDB 支援地理空間資料的鄰近、包含和交集查詢。鄰近查詢的一個很好的例子是找到所有距離小於某個距離且距離多於另一個點(城市)的點(所有機場)。包含查詢的一個很好的例子是找到位於指定區域/多邊形(紐約州)中的所有點(所有機場)。交叉查詢的一個很好的例子是找到與點(城市)相交的多邊形(州)。您可以使用以下地理空間運營商從數據中獲得見解。
-
$nearSphere
-$nearSphere
是一個查找運算符,支持從地理JSON點最近到最遠找到點。 -
$geoNear
-$geoNear
是一個聚合運算符,支持從地理JSON點計算以米為單位的距離。 -
$minDistance
-$minDistance
是一個查找運算符,與$nearSphere
或$geoNear
一起使用,用於過濾距中心點至少指定最小距離的文檔。 -
$maxDistance
-$maxDistance
是與$nearSphere
或一起使用的查找運算符,$geoNear
用於過濾距中心點指定最大距離的文檔。 -
$geoWithin
-$geoWithin
是一個查找運算符,支持查找具有完全存在於指定形狀(如多邊形)中的地理空間數據的文檔。 -
$geoIntersects
-$geoIntersects
是一個查找運算符,支持查找其地理空間數據與指定 Geo JSON 對象相交的文檔。
注意
$geoNear
並在您的鄰近查詢中使用的 Geo JSON 欄位上$nearSphere
需要 2dsphere 索引。
範例 1
在此示例中,您將學習如何找到從地址(點)最近的距離排序的所有餐廳(點)。
若要執行此類查詢,您可以使$geoNear
用計算與另一個點之間的點集合距離。您也可以加入以測量distanceMultiplier
以公里為單位的距離。
db.usarestaurants.aggregate([ { "$geoNear":{ "near":{ "type":"Point", "coordinates":[ -122.3516, 47.6156 ] }, "spherical":true, "distanceField":"DistanceKilometers", "distanceMultiplier":0.001 } } ])
上面的命令將返回按照距指定點的距離(最接近最遠)排序的餐廳。這個命令的輸出看起來像這樣
{ "_id" : ObjectId("611f3da985009a81ad38e74b"), "state" : "Washington", "city" : "Seattle", "name" : "Noodle House", "rating" : 4.8, "location" : { "type" : "Point", "coordinates" : [ -122.3517, 47.6159 ] }, "DistanceKilometers" : 0.03422834547294996 } { "_id" : ObjectId("611f3da185009a81ad38e74a"), "state" : "Washington", "city" : "Seattle", "name" : "Thai Palace", "rating" : 4.8, "location" : { "type" : "Point", "coordinates" : [ -122.3264, 47.6009 ] }, "DistanceKilometers" : 2.5009390081704277 } { "_id" : ObjectId("611f3dae85009a81ad38e74c"), "state" : "Washington", "city" : "Seattle", "name" : "Curry House", "rating" : 4.8, "location" : { "type" : "Point", "coordinates" : [ -121.4517, 47.6229 ] }, "DistanceKilometers" : 67.52845344856914 }
若要限制查詢中的結果數目,請使用limit
或num
選項。
limit
:
db.usarestaurants.aggregate([ { "$geoNear":{ "near":{ "type":"Point", "coordinates":[ -122.3516, 47.6156 ] }, "spherical":true, "distanceField":"DistanceKilometers", "distanceMultiplier":0.001, "limit": 10 } } ])
num
:
db.usarestaurants.aggregate([ { "$geoNear":{ "near":{ "type":"Point", "coordinates":[ -122.3516, 47.6156 ] }, "spherical":true, "distanceField":"DistanceKilometers", "distanceMultiplier":0.001, "num": 10 } } ])
注意
$geoNear
stage 支援limit
和選num
項,以指定要傳回的文件數目上限。 $geoNear
如果未指定limit
或num
選項,依預設,最多會傳回 100 個文件。如果存在且值小於 100,則$limit
階段值會覆寫此值。
範例 2
在此示例中,您將學習如何找到特定地址(點)2 公里內的所有餐廳(點)。要執行此類查詢,您可以$nearSphere
在 Geo JSON Point 的最小$minDistance
和最大值$maxDistance
內使用
db.usarestaurants.find({ "location":{ "$nearSphere":{ "$geometry":{ "type":"Point", "coordinates":[ -122.3516, 47.6156 ] }, "$minDistance":1, "$maxDistance":2000 } } }, { "name":1 })
上面的命令將返回距指定點最大距離 2 公里的餐廳。這個命令的輸出看起來像這樣
{ "_id" : ObjectId("611f3da985009a81ad38e74b"), "name" : "Noodle House" }
限制
Amazon DocumentDB 不支援多邊形、、、 LineString MultiPoint、 MultiPolygon和的查詢或索引。 MultiLineString GeometryCollection