Database Manual / Reference / Query Language / Query Predicates / Geospatial

$box

Definition定义

$box

Specifies a rectangle for a geospatial $geoWithin query to return documents that are within the bounds of the rectangle, according to their point-based location data. 地理空间$geoWithin查询指定一个矩形,以根据其基于点的位置数据返回矩形边界内的文档。When used with the $box operator, $geoWithin returns documents based on grid coordinates and does not query for GeoJSON shapes.当与$box运算符一起使用时,$geoWithin会根据网格坐标返回文档,而不会查询GeoJSON形状。

To use the $box operator, you must specify the bottom left and top right corners of the rectangle in an array object:要使用$box运算符,您必须在数组对象中指定矩形的左下角和右上角:

{
<location field>: {
$geoWithin: {
$box: [
[ <bottom left coordinates> ],
[ <upper right coordinates> ]
]
}
}
}

Important

If you use longitude and latitude, specify longitude first.如果使用经度和纬度,请先指定经度。

Behavior行为

The query calculates distances using flat (planar) geometry.该查询使用平面几何图形计算距离。

Applications can use $box without having a geospatial index. However, geospatial indexes support much faster queries than the unindexed equivalents.应用程序可以在没有地理空间索引的情况下使用$box。然而,地理空间索引支持的查询速度比未索引的同类索引快得多。

Only the 2d geospatial index supports $box.只有2d地理空间索引支持$box

Example示例

The following example query returns all documents that are within the box having points at: [ 0 , 0 ], [ 0 , 100 ], [ 100 , 0 ], and [ 100 , 100 ].以下示例查询返回框内具有以下点的所有文档:[0,0][0,100][100,0][100,100]

db.places.find( {
loc: { $geoWithin: { $box: [ [ 0, 0 ], [ 100, 100 ] ] } }
} )