OPTIONS
翻译或纠错本页面

$polygon

$polygon

1.9 新版功能.

The $polygon operator specifies a polygon for a geospatial $geoWithin query on legacy coordinate pairs. The query returns pairs that are within the bounds of the polygon. The operator does not query for GeoJSON objects.

The $polygon operator calculates distances using flat (planar) geometry.

The 2d geospatial index supports the $polygon operator.

To define the polygon, specify an array of coordinate points. Use the following syntax:

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

重要

If you use longitude and latitude, specify longitude first.

The last point specified is always implicitly connected to the first. You can specify as many points, and therefore sides, as you like.

The following query returns all documents that have coordinates that exist within the polygon defined by [ 0 , 0 ], [ 3 , 6 ], and [ 6 , 0 ]:

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

在 2.2.3 版更改: Applications can use $polygon without having a geospatial index. However, geospatial indexes support much faster queries than the unindexed equivalents. Before 2.2.3, a geospatial index must exist on a field holding coordinates before using any of the geospatial query operators.

←   $box $uniqueDocs  →
ON THIS PAGE