$within¶
-
$within¶ The
$withinoperator allows you to select items that exist within a shape on a coordinate system for geospatial queries. This operator uses the following syntax:Replace
{ shape }with a document that describes a shape. The$withincommand supports the following shapes. These shapes and the relevant expressions follow:Rectangles. Use the
$boxoperator, consider the following variable and$withindocument:Here a box,
[[100,120], [100,0]]describes the parameter for the query. As a minimum, you must specify the lower-left and upper-right corners of the box.Circles. Use the
$centeroperator. Specify circles in the following form:Circular distance on a sphere. Use the
$centerSphereoperator. For the syntax, see$centerSphere.Polygons. Use the
$polygonoperator. Specify polygons with an array of points. See the following example:The last point of a polygon is implicitly connected to the first point.
All shapes include the border of the shape as part of the shape, although this is subject to the imprecision of floating point numbers.
Use
$uniqueDocsto control whether documents with many location fields show up multiple times when more than one of its fields match the query.Changed in version 2.2.3: Before 2.2.3, a geospatial index must exist on a field holding coordinates before using any of the geolocation query operators. After 2.2.3, applications may use geolocation query operators without having a geospatial index; however, geospatial indexes will support much faster geospatial queries than the unindexed equivalents.
Note
A geospatial index must exist on a field and the field must hold coordinates before you can use any of the geolocation query operators.