本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 Amazon DocumentDB 查询地理空间数据
本节介绍了如何使用 Amazon DocumentDB 查询地理空间数据。阅读本节后,您将能够回答如何在 Amazon DocumentDB 中存储、查询和索引地理空间数据。
概览
地理空间的常见用例包括从数据中进行邻近分析。例如,“查找西雅图 50 英里范围内的所有机场”,或 “从给定地点找到最近的餐厅”。Amazon DocumentDB 使用GeoJSON 规范
索引和存储地理空间数据
Amazon DocumentDB 使用 “Point” GeoJSON 类型来存储地理空间数据。每个 GeoJSON 文档(或子文档)通常由两个字段组成:
-
类型-表示的形状,通知 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
是一个查找运算符,支持从距离 GeoJSON 点最近到最远的点查找点。 -
$geoNear
-$geoNear
是一种聚合运算符,支持计算距 GeoJSON 点的距离(以米为单位)。 -
$minDistance
-$minDistance
是与结合使用的查找运算符$nearSphere
要么$geoNear
以筛选至少与中心点指定的最小距离的文档。 -
$maxDistance
-$maxDistance
是与结合使用的查找运算符$nearSphere
要么$geoNear
以筛选最多距离中心点指定最大距离的文档。 -
$geoWithin
-$geoWithin
是一个查找运算符,支持查找具有完全存在于指定形状(如多边形)内的地理空间数据的文档。 -
$geoIntersects
-$geoIntersects
是一个查找运算符,支持查找其地理空间数据与指定 GeoJSON 对象交叉的文档。
$geoNear
和$nearSphere
需要在邻近查询中使用的 GeoJSON 字段上的 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 }
示例 2
在此示例中,您将学习如何在距离特定地址(点)2 公里范围内找到所有餐厅(积分)。要执行这样的查询,你可以使用$nearSphere
最小值$minDistance
最大值$maxDistance
来自 GeoJSON 点
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、MultiPOLYGON、MultiLineString 和 GeometryCollection。