本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
$geoWithin
Amazon DocumentDB 中的$geoWithin运算符用于查找位置数据(表示为 GeoJSON 对象)完全包含在指定形状(例如多边形或多面)内的文档。这对于查询位于特定地理区域内的对象非常有用。
参数
-
$geometry: 一个 GeoJSON 对象,它表示要查询的形状。
示例(MongoDB 外壳)
以下示例演示如何使用$geoWithin运算符查找位于纽约州内的所有机场。
创建示例文档
// Insert state document db.states.insert({ "name": "New York", "loc": { "type": "Polygon", "coordinates": [[ [-79.76278, 45.0], [-73.94, 45.0], [-73.94, 40.5], [-79.76278, 40.5], [-79.76278, 45.0] ]] } }); // Insert airport documents db.airports.insert([ { "name": "John F. Kennedy International Airport", "type": "airport", "code": "JFK", "loc": { "type": "Point", "coordinates": [-73.7781, 40.6413] } }, { "name": "LaGuardia Airport", "type": "airport", "code": "LGA", "loc": { "type": "Point", "coordinates": [-73.8772, 40.7769] } }, { "name": "Buffalo Niagara International Airport", "type": "airport", "code": "BUF", "loc": { "type": "Point", "coordinates": [-78.7322, 42.9403] } } ]);
查询示例
var state = db.states.findOne({"name": "New York"}); db.airports.find({ "loc": { "$geoWithin": { "$geometry": state.loc } } }, { "name": 1, "type": 1, "code": 1, "_id": 0 });
输出
[
{
"name": "John F. Kennedy International Airport",
"type": "airport",
"code": "JFK"
},
{
"name": "LaGuardia Airport",
"type": "airport",
"code": "LGA"
}
]
代码示例
要查看使用该$geoWithin命令的代码示例,请选择要使用的语言的选项卡: