运算符 $polygon
为地理空间查询定义多边形,使你可以在不规则的形状中找到位置。 运算符可用于查询复杂地理边界内的位置。
语法
{
<location field>: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[[<longitude1>, <latitude1>], ..., [<longitudeN>, <latitudeN>], [<longitude1>, <latitude1>]]
]
}
}
}
}
参数
参数 | DESCRIPTION |
---|---|
location field |
包含地理空间数据的字段 |
coordinates |
构成多边形的坐标对数组。 第一个和最后一个点必须与关闭多边形相同 |
示例:
查询根据提供的坐标检索属于自定义多边形区域的存储。
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)
查询返回两个位于该区域内的存储区。
{
"_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]
}
}
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。