Navigation
This version of the documentation is archived and no longer supported.

$box

$box

New in version 1.4.

The $box operator specifies a rectangle for a geospatial $geoWithin query. The query returns documents that are within the bounds of the rectangle, according to their point-based location data. The $box operator returns documents based on grid coordinates and does not query for GeoJSON shapes.

The query calculates distances using flat (planar) geometry. The 2d geospatial index supports the $box operator.

To use the $box operator, you must specify the bottom left and top right corners of the rectangle in an array object. Use the following syntax:

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

Important

If you use longitude and latitude, specify longitude first.

The following example query returns all documents that are within the box having points at: [ 0 , 0 ], [ 0 , 100 ], [ 100 , 0 ], and [ 100 , 100 ].

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

Changed in version 2.2.3: Applications can use $box without having a geospatial index. However, geospatial indexes support much faster queries than the unindexed equivalents. Before 2.2.3, a geospatial index must exist on a field holding coordinates before using any of the geospatial query operators.

←   $centerSphere $polygon  →