Definition定义
$geoWithinSelects documents with geospatial data that exists entirely within a specified shape.选择具有完全存在于指定形状内的地理空间数据的文档。The specified shape can be either a GeoJSON指定的形状可以是GeoJSON多边形(单环或多环)、GeoJSON多边形或由传统坐标对定义的形状。Polygon(either single-ringed or multi-ringed), a GeoJSONMultiPolygon, or a shape defined by legacy coordinate pairs.The$geoWithinoperator uses the$geometryoperator to specify the GeoJSON object.$geoWithin运算符使用$geometry运算符指定GeoJSON对象。To specify a GeoJSON polygons or multipolygons using the default coordinate reference system (CRS), use the following syntax:要使用默认坐标参考系(CRS)指定GeoJSON多边形或多多边形,请使用以下语法:{
<location field>: {
$geoWithin: {
$geometry: {
type: <"Polygon" or "MultiPolygon"> ,
coordinates: [ <coordinates> ]
}
}
}
}For对于指定面积大于单个半球的GeoJSON几何体的$geoWithinqueries that specify GeoJSON geometries with areas greater than a single hemisphere, the use of the default CRS results in queries for the complementary geometries.$geoWithin查询,使用默认CRS会导致对互补几何体的查询。To specify a single-ringed GeoJSON polygon with a custom MongoDB CRS, use the following prototype that specifies the custom MongoDB CRS in the要使用自定义MongoDB CRS指定一个单环GeoJSON多边形,请使用以下原型在$geometryexpression:$geometry表达式中指定自定义MongoDB CRS:{
<location field>: {
$geoWithin: {
$geometry: {
type: "Polygon" ,
coordinates: [ <coordinates> ],
crs: {
type: "name",
properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
}
}
}
}
}The custom MongoDB CRS uses a counter-clockwise winding order and allows自定义MongoDB CRS使用逆时针缠绕顺序,并允许$geoWithinto support queries with a single-ringed GeoJSON polygon whose area is greater than or equal to a single hemisphere.$geoWithin支持使用面积大于或等于单个半球的单个环形GeoJSON多边形的查询。If the specified polygon is smaller than a single hemisphere, the behavior of如果指定的多边形小于单个半球,则$geoWithinwith the MongoDB CRS is the same as with the default CRS. See also "Big" Polygons.$geoWithin在MongoDB CRS中的行为与默认CRS相同。另请参见“大”多边形。If querying for inclusion in a shape defined by legacy coordinate pairs on a plane, use the following syntax:如果查询是否包含在由平面上的传统坐标对定义的形状中,请使用以下语法:{
<location field>: {
$geoWithin: { <shape operator>: <coordinates> }
}
}The available shape operators are:可用的形状运算符有:$box,$polygon,$center(defines a circle), and(定义一个圆),以及$centerSphere(defines a circle on a sphere).(定义球体上的圆)。
Important
If you use longitude and latitude, specify coordinates in order of如果使用经度和纬度,请按longitude, latitude.longitude, latitude的顺序指定坐标。
Behavior行为
Geospatial Indexes地理空间索引
$geoWithin does not require a geospatial index. However, a geospatial index will improve query performance. 不需要地理空间索引。然而,地理空间索引将提高查询性能。Both 2dsphere and 2d geospatial indexes support $geoWithin.2dsphere和2d地理空间索引都支持$geoWithin。
Unsorted Results未排序的结果
The $geoWithin operator does not return sorted results. $geoWithin运算符不返回排序结果。As such, MongoDB can return 因此,MongoDB可以比对结果进行排序的地理空间$geoWithin queries more quickly than geospatial $near or $nearSphere queries, which sort results.$near或$nearSphere查询更快地返回$geoWithin查询。
Degenerate Geometry退化几何
$geoWithin does not guarantee that it will consider a piece of geometry to contain its component geometry, or another polygon sharing its component geometry.$geoWithin不保证它会将一块几何图形视为包含其组件几何图形,也不保证将另一个共享其组件几何图元的多边形视为包含该几何图形。
"Big" Polygons“大”多边形
For 对于$geoWithin, if you specify a single-ringed polygon that has an area greater than a single hemisphere, include the custom MongoDB coordinate reference system in the $geometry expression. $geoWithin,如果指定面积大于单个半球的单个环形多边形,请在$geometry表达式中包含自定义MongoDB坐标参考系。Otherwise, 否则,$geoWithin queries for the complementary geometry. For all other GeoJSON polygons with areas greater than a hemisphere, $geoWithin queries for the complementary geometry.$geoWithin将查询互补几何图形。对于面积大于半球的所有其他GeoJSON多边形,$geoWithin查询互补几何体。
Examples示例
Within a Polygon多边形内
The following example selects all 以下示例选择完全存在于GeoJSONloc data that exist entirely within a GeoJSON Polygon. Polygon中的所有位置数据。The area of the polygon is less than the area of a single hemisphere:多边形的面积小于单个半球的面积:
db.places.find(
{
loc: {
$geoWithin: {
$geometry: {
type : "Polygon" ,
coordinates: [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ]
}
}
}
}
)
For single-ringed polygons with areas greater than a single hemisphere, see Within a "Big" Polygon.对于面积大于单个半球的单环形多边形,请参阅“大”多边形内。
Within a "Big" Polygon在“大”多边形内
To query with a single-ringed GeoJSON polygon whose area is greater than a single hemisphere, the 要使用面积大于单个半球的单个环形GeoJSON多边形进行查询,$geometry expression must specify the custom MongoDB coordinate reference system. For example:$geometry表达式必须指定自定义MongoDB坐标参考系。例如:
db.places.find(
{
loc: {
$geoWithin: {
$geometry: {
type : "Polygon" ,
coordinates: [
[
[ -100, 60 ], [ -100, 0 ], [ -100, -60 ], [ 100, -60 ], [ 100, 60 ], [ -100, 60 ]
]
],
crs: {
type: "name",
properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
}
}
}
}
}
)