On this page本页内容
$polygon
Specifies a polygon for a geospatial 为传统坐标对上的geospacial$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.您可以指定任意多个点,即边。
The $polygon
operator calculates distances using flat (planar) geometry.$polygon
运算符使用平面几何体计算距离。
Applications can use 应用程序可以使用$polygon
without having a geospatial index. $polygon
而无需地理空间索引。However, geospatial indexes support much faster queries than the unindexed equivalents.但是,地理空间索引支持的查询速度比未编制索引的等效查询快得多。
Only the 2d geospatial index supports the 只有$polygon
operator.2d
地理空间索引支持$polygon
运算符。
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 ] ] } } } )