OPTIONS
翻译或纠错本页面

$geoWithin

$geoWithin

2.4 新版功能: $geoWithin replaces $within which is deprecated.

The $geoWithin operator is a geospatial query operator that queries for a defined point, line or shape that exists entirely within another defined shape. When determining inclusion, MongoDB considers the border of a shape to be part of the shape, subject to the precision of floating point numbers.

The $geoWithin operator queries for inclusion in a GeoJSON polygon or a shape defined by legacy coordinate pairs.

The $geoWithin operator does not return sorted results. As a result MongoDB can return $geoWithin queries more quickly than geospatial $near or $nearSphere queries, which sort results.

The 2dsphere and 2d indexes both support the $geoWithin operator.

在 2.2.3 版更改: $geoWithin does not require a geospatial index. However, a geospatial index will improve query performance.

If querying for geometries that exist within a GeoJSON polygon on a sphere, pass the polygon to $geoWithin using the $geometry operator.

For a polygon with only an exterior ring use following syntax:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                            { $geometry :
                               { type : "Polygon" ,
                                 coordinates : [ [ [ <lng1>, <lat1> ] , [ <lng2>, <lat2> ] ... ] ]
                      } } } } )

重要

Specify coordinates in longitude, latitude order.

For a polygon with an exterior and interior ring use following syntax:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                            { $geometry :
                               { type : "Polygon" ,
                                 coordinates : [ [ [ <lng1>, <lat1> ] , [ <lng2>, <lat2> ] ... ]
                                                 [ [ <lngA>, <latA> ] , [ <lngB>, <latB> ] ... ] ]
                      } } } } )

The following example selects all indexed points and shapes that exist entirely within a GeoJSON polygon:

db.places.find( { loc :
                  { $geoWithin :
                    { $geometry :
                      { type : "Polygon" ,
                        coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ]
                } } } } )

If querying for inclusion in a shape defined by legacy coordinate pairs on a plane, use the following syntax:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                            { <shape operator> : <coordinates>
                      } } } )

For the syntax of shape operators, see: $box, $polygon, $center (defines a circle), and $centerSphere (defines a circle on a sphere).

注解

Any geometry specified with GeoJSON to $geoWithin queries, must fit within a single hemisphere. MongoDB interprets geometries larger than half of the sphere as queries for the smaller of the complementary geometries.

$within

2.4 版后已移除: $geoWithin replaces $within in MongoDB 2.4.

ON THIS PAGE