$box
Defines a rectangular area for geospatial queries using two coordinate pairs, useful for finding locations within a rectangular geographical boundary.
Syntax
{ <location field>: { $geoWithin: { $box: [<lower_left>, <upper_right>] } } }
Parameters
location field
stringrequiredThe field containing the geospatial data
lower_left
objectrequiredArray of [longitude, latitude] specifying the bottom-left corner
upper_right
objectrequiredArray of [longitude, latitude] specifying the top-right corner
Examples
Find stores within rectangular area
Find stores located within a specific rectangular area defined by coordinate pairs
Query:
db.stores.find({ location: { $geoWithin: { $box: [[-142.0012, -51.3041], [123.3403, 70.1272]] } } })
Output:
Documents where location falls within the specified rectangular boundary