定义
$boxSpecifies a rectangle for a geospatial
$geoWithinquery to return documents that are within the bounds of the rectangle, according to their point-based location data. When used with the$boxoperator,$geoWithinreturns documents based on grid coordinates and does not query for GeoJSON shapes.To use the
$boxoperator, you must specify the bottom left and top right corners of the rectangle in an array object:{ <location field>: { $geoWithin: { $box: [ [ <bottom left coordinates> ], [ <upper right coordinates> ] ] } } } 重要
如果使用经度和纬度,请先指定经度。
行为
查询用平面几何图形计算距离。
Applications can use $box without having a geospatial index. However, geospatial indexes support much faster queries than the unindexed equivalents.
例子
以下示例查询返回框中具有以下点的所有文档:[ 0 , 0 ]、[ 0 , 100 ]、[ 100 , 0 ] 和 [ 100 , 100 ]。
db.places.find( { loc: { $geoWithin: { $box: [ [ 0, 0 ], [ 100, 100 ] ] } } } )