Docs HomeMongoDB Manual

$polygon

On this page本页内容

Definition定义

$polygon

Specifies a polygon for a geospatial $geoWithin query on legacy coordinate pairs. 为旧坐标对上的地理空间$geoWithin查询指定多边形。The query returns pairs that are within the bounds of the polygon. The operator does not query for GeoJSON objects.查询返回在多边形边界内的对。运算符不查询GeoJSON对象。

To define the polygon, specify an array of coordinate points:要定义多边形,请指定坐标点数组:

{
<location field>: {
$geoWithin: {
$polygon: [ [ <x1> , <y1> ], [ <x2> , <y2> ], [ <x3> , <y3> ], ... ]
}
}
}

The last point is always implicitly connected to the first. You can specify as many points, i.e. sides, as you like.最后一点总是隐式地连接到第一点。可以指定任意多个点,即边。

Important

If you use longitude and latitude, specify longitude first.如果使用经度和纬度,请先指定经度。

Behavior行为

The $polygon operator calculates distances using flat (planar) geometry.$polygon运算符使用平面(平面)几何图形计算距离。

Applications can use $polygon without having a geospatial index. However, geospatial indexes support much faster queries than the unindexed equivalents.应用程序可以在没有地理空间索引的情况下使用$polygon。但是,地理空间索引支持的查询速度比未编制索引的索引快得多。

Only the 2d geospatial index supports the $polygon operator.只有2d地理空间索引支持$polygon运算符。

Example实例

The following query returns all documents that have coordinates that exist within the polygon defined by [ 0 , 0 ], [ 3 , 6 ], and [ 6 , 0 ]:以下查询返回坐标位于[ 0 , 0 ][ 3 , 6 ][ 6 , 0 ]定义的多边形内的所有文档:

db.places.find(
{
loc: {
$geoWithin: { $polygon: [ [ 0 , 0 ], [ 3 , 6 ], [ 6 , 0 ] ] }
}
}
)