你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用对象: MongoDB vCore
运算符 $polygon
为地理空间查询定义多边形,使你可以在不规则的形状中找到位置。 这对于查询复杂地理边界内的位置特别有用。
语法
运算符的 $isArray
语法如下所示:
{
<location field>: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[[<longitude1>, <latitude1>], ..., [<longitudeN>, <latitudeN>], [<longitude1>, <latitude1>]]
]
}
}
}
}
参数
参数 | 类型 | DESCRIPTION |
---|---|---|
location field |
领域 | 包含地理空间数据的字段 |
coordinates |
数组 | 构成多边形的坐标对数组。 第一个和最后一个点必须与关闭多边形相同 |
示例:
stores
使用集合,让我们在由三个商店位置构成的三角区域中查找存储:
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
})