Definition
geoWithinThe
geoWithinoperator supports querying geographic points within a given geometry. Only points are returned, even ifindexShapesvalue istruein the index definition.You can query points within a:
Circle
Bounding box
Polygon
When specifying the coordinates to search, longitude must be specified first and then the latitude. Longitude values can be between
-180and180, both inclusive. Latitude values can be between-90and90, both inclusive. Coordinate values can be integers or doubles.Note
MongoDB Search does not support the following:
Non-default coordinate reference system (CRS)
Planar XY coordinate system (2 dimensional)
Coordinate pairs Point notation (that is,
pointFieldName: [12, 34])
Syntax
geoWithin has the following syntax:
{ "$search": { "index": <index name>, // optional, defaults to "default" "geoWithin": { "path": "<field-to-search>", "box | circle | geometry": <object>, "score": <score-options> } } }
Options
geoWithin uses the following terms to construct a query:
Field | Type | Description | Necessity |
|---|---|---|---|
| object | Object that specifies the bottom left and top right GeoJSON points of a box to search within. The object takes the following fields: To learn how to specify GeoJSON data inside a GeoJSON object, see GeoJSON Objects. Either | conditional |
| object | Object that specifies the center point and the radius in meters to search within. The object contains the following GeoJSON fields:
To learn how to specify GeoJSON data inside a GeoJSON object, see GeoJSON Objects. Either | conditional |
| GeoJSON object | GeoJSON object that specifies the MultiPolygon or Polygon to search within. The polygon must be specified as a closed loop where the last position is the same as the first position. When calculating geospatial results, MongoDB Search geoShape and geoWithin operators and MongoDB $geoIntersects operator use different geometries. This difference can be seen in how MongoDB Search and MongoDB draw polygonal edges. MongoDB Search draws polygons based on Cartesian distance, which is the shortest line between two points in the coordinate reference system. MongoDB draws polygons using the geodesic mode based on 2dsphere indexes that is built on top of a third-party library for geodesic types, or the flat mode, from 2d indexes. To learn more, see GeoJSON Objects. MongoDB Search and MongoDB could return different results for geospatial queries involving polygons. To learn how to specify GeoJSON data inside a GeoJSON object, see GeoJSON Objects. Either | conditional |
| string or array of strings | Indexed geo type field or fields to search. | yes |
| object | Score to assign to matching search results. By default, the
score in the results is
For information on using | no |
Examples
The following examples use the listingsAndReviews collection in the
sample_airbnb database. If you have the sample dataset on your cluster, you can create a custom
MongoDB Search index for geo type and run the
example queries on your cluster.
Use the following sample index definition for
indexing the address.location field in the listingsAndReviews
collection:
1 { 2 "mappings": { 3 "fields": { 4 "address": { 5 "fields": { 6 "location": { 7 "type": "geo" 8 } 9 }, 10 "type": "document" 11 }, 12 "property_type": { 13 "type": "token" 14 } 15 } 16 } 17 }
box Example
The following query uses the geoWithin operator with the box field to search
for properties within a bounding box in Australia.
The query includes a:
$limitstage to limit the output to3results.$projectstage to exclude all fields exceptnameandaddress.
Note
You don't need to specify indexes named default in your MongoDB Search
query. If your index has any other name, you must specify the
index field.
Basic Example
Metadata Example
circle Example
The following query uses the geoWithin operator with the circle field to search for properties within one mile radius of specified coordinates in Canada.
The query includes a:
$limitstage to limit the output to3results.$projectstage to exclude all fields exceptnameandaddress, and for Compass examples, the required steps reference.
Note
You don't need to specify indexes named default in your MongoDB Search
query. If your index has any other name, you must specify the
index field.
geometry Examples
The following examples use the geoWithin operator with the
geometry field to search for properties in Hawaii. The type field
specifies whether the area is a GeoJSON Polygon or
MultiPolygon.
Note
You don't need to specify indexes named default in your MongoDB Search
query. If your index has any other name, you must specify the
index field.