$geoWithin

在本页面

Definition

指定的形状可以是 GeoJSON Polygon(单环或多环),GeoJSON MultiPolygon或旧版坐标对定义的形状。 $geoWithin运算符使用$geometry运算符指定GeoJSON对象。

要使用默认坐标参考系统(CRS)指定 GeoJSON 多边形或多多边形,请使用以下语法:

{
   <location field>: {
      $geoWithin: {
         $geometry: {
            type: <"Polygon" or "MultiPolygon"> ,
            coordinates: [ <coordinates> ]
         }
      }
   }
}

对于指定面积大于单个半球的 GeoJSON 几何的$geoWithin查询,使用默认 CRS 会生成对互补几何的查询。

3.0 版中的新功能:要使用自定义 MongoDB CRS 指定单环 GeoJSON polygon,请使用以下原型,该原型在$geometry表达式中指定自定义 MongoDB CRS:

{
   <location field>: {
      $geoWithin: {
         $geometry: {
           type: "Polygon" ,
           coordinates: [ <coordinates> ],
           crs: {
              type: "name",
              properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
           }
         }
      }
   }
}

自定义 MongoDB CRS 使用逆时针缠绕 Sequences,并允许$geoWithin支持面积大于或等于一个半球的单环 GeoJSON polygon的查询。如果指定的多边形小于单个半球,则 MongoDB CRS 的$geoWithin行为与默认 CRS 相同。另请参见"Big" Polygons

如果要查询包含在平面上的传统坐标对定义的形状中,请使用以下语法:

{
   <location field>: {
      $geoWithin: { <shape operator>: <coordinates> }
   }
}

可用的形状运算符为:

Important

如果使用经度和纬度,请按longitude, latitude的 Sequences 指定坐标。

Behavior

Geospatial Indexes

$geoWithin不需要地理空间索引。但是,地理空间索引将提高查询性能。 2dsphere2d地理空间索引均支持$geoWithin

Unsorted Results

$geoWithin运算符不返回排序结果。这样,与对结果进行排序的地理空间$near$nearSphere查询相比,MongoDB 可以更快地返回$geoWithin查询。

Degenerate Geometry

$geoWithin不保证它将考虑一个几何图形包含其组件几何图形,或另一个共享其组件几何图形的多边形。

"Big" Polygons

对于$geoWithin,如果指定面积大于单个半球的单环多边形,则包含$ geometry 中的自定义 MongoDB 坐标参考系统表达式;否则,$geoWithin查询互补几何。对于面积大于半球的所有其他 GeoJSON 多边形,$geoWithin查询互补几何。

Examples

在多边形内

以下示例选择了完全存在于 GeoJSON Polygon中的所有loc数据。多边形的面积小于单个半球的面积:

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

对于面积大于单个半球的单环多边形,请参见在“大”多边形内

在“大”多边形内

要使用面积大于单个半球的单环 GeoJSON 多边形进行查询,$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" }
             }
          }
       }
     }
   }
)
首页