On this page
$geoIntersects
在本页面
Definition
$geoIntersects
- 选择地理空间数据与指定的GeoJSON对象相交的文档;即数据和指定对象的交集为非空。
$geoIntersects运算符使用$geometry运算符指定GeoJSON对象。要使用默认坐标参考系统(CRS)指定 GeoJSON 多边形或多多边形,请使用以下语法:
{
<location field>: {
$geoIntersects: {
$geometry: {
type: "<GeoJSON object type>" ,
coordinates: [ <coordinates> ]
}
}
}
}
对于指定面积大于单个半球的 GeoJSON 几何的$geoIntersects查询,使用默认 CRS 会生成对互补几何的查询。
3.0 版中的新功能:要使用自定义 MongoDB CRS 指定单环 GeoJSON polygon,请使用以下原型,该原型在$geometry表达式中指定自定义 MongoDB CRS:
{
<location field>: {
$geoIntersects: {
$geometry: {
type: "Polygon" ,
coordinates: [ <coordinates> ],
crs: {
type: "name",
properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
}
}
}
}
}
自定义 MongoDB CRS 使用逆时针缠绕 Sequences,并允许$geoIntersects支持面积大于或等于一个半球的单环 GeoJSON polygon的查询。如果指定的多边形小于单个半球,则 MongoDB CRS 的$geoIntersects行为与默认 CRS 相同。另请参见"Big" Polygons。
Important
如果指定纬度和经度坐标,请先列出 经度 ,然后列出 latitude :
有效的经度值在
-180
和180
之间(包括两端值)。有效的纬度值在
-90
和90
之间(包括两端值)。
Behavior
Geospatial Indexes
$geoIntersects使用球形几何。 $geoIntersects不需要地理空间索引。但是,地理空间索引将提高查询性能。仅2dsphere地理空间索引支持$geoIntersects。
Degenerate Geometry
$geoIntersects不保证会考虑多边形与其自己的边相交;它自己的顶点;或另一个共享顶点或边线但没有内部空间的多边形。
"Big" Polygons
对于$geoIntersects,如果指定面积大于单个半球的单环多边形,则包含$ geometry 中的自定义 MongoDB 坐标参考系统表达式;否则,$geoIntersects查询互补几何。对于面积大于半球的所有其他 GeoJSON 多边形,$geoIntersects查询互补几何。
Examples
与多边形相交
下面的示例使用$geoIntersects选择与coordinates
数组定义的Polygon相交的所有loc
数据。多边形的面积小于单个半球的面积:
db.places.find(
{
loc: {
$geoIntersects: {
$geometry: {
type: "Polygon" ,
coordinates: [
[ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ]
]
}
}
}
}
)
对于面积大于单个半球的单环多边形,请参见与“大”多边形相交。
与“大”多边形相交
要使用面积大于单个半球的单环 GeoJSON 多边形进行查询,$geometry表达式必须指定自定义 MongoDB 坐标参考系统。例如:
db.places.find(
{
loc: {
$geoIntersects: {
$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" }
}
}
}
}
}
)