> This page location: /docs.md > /docs/development.md > /docs/manual/reference/mql.md > /docs/manual/reference/mql/query-predicates.md > /docs/manual/reference/mql/query-predicates/geospatial.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $geoWithin (query predicate operator)

## Definition

Selects documents with geospatial data that exists entirely within a specified shape.

The specified shape can be either a GeoJSON [`Polygon`](https://www.mongodb.com/docs/manual/reference/geojson.md#std-label-geojson-polygon) (either single-ringed or multi-ringed), a GeoJSON [`MultiPolygon`](https://www.mongodb.com/docs/manual/reference/geojson.md#std-label-geojson-multipolygon), or a shape defined by legacy coordinate pairs. The [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) operator uses the [`$geometry`](https://www.mongodb.com/docs/manual/reference/operator/query/geometry.md#mongodb-query-op.-geometry) operator to specify the [GeoJSON](https://www.mongodb.com/docs/manual/reference/geojson.md#std-label-geospatial-indexes-store-geojson) object.

To specify a GeoJSON polygons or multipolygons using the default coordinate reference system (CRS), use the following syntax:

```javascript
{
   <location field>: {
      $geoWithin: {
         $geometry: {
            type: <"Polygon" or "MultiPolygon"> ,
            coordinates: [ <coordinates> ]
         }
      }
   }
}
```

For [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) queries that specify GeoJSON geometries with areas greater than a single hemisphere, the use of the default CRS results in queries for the complementary geometries.

To specify a single-ringed GeoJSON [polygon](https://www.mongodb.com/docs/manual/reference/geojson.md#std-label-geojson-polygon) with a custom MongoDB CRS, use the following prototype that specifies the custom MongoDB CRS in the [`$geometry`](https://www.mongodb.com/docs/manual/reference/operator/query/geometry.md#mongodb-query-op.-geometry) expression:

```javascript
{
   <location field>: {
      $geoWithin: {
         $geometry: {
           type: "Polygon" ,
           coordinates: [ <coordinates> ],
           crs: {
              type: "name",
              properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
           }
         }
      }
   }
}
```

The custom MongoDB CRS uses a counter-clockwise winding order and allows [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) to support queries with a single-ringed GeoJSON [polygon](https://www.mongodb.com/docs/manual/reference/geojson.md#std-label-geojson-polygon) whose area is greater than or equal to a single hemisphere. If the specified polygon is smaller than a single hemisphere, the behavior of [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) with the MongoDB CRS is the same as with the default CRS. See also ["Big" Polygons.](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#std-label-geowithin-big-poly)

If querying for inclusion in a shape defined by legacy coordinate pairs on a plane, use the following syntax:

```javascript
{
   <location field>: {
      $geoWithin: { <shape operator>: <coordinates> }
   }
}
```

The available shape operators are:

- [`$box`,](https://www.mongodb.com/docs/manual/reference/operator/query/box.md#mongodb-query-op.-box)

- [`$polygon`,](https://www.mongodb.com/docs/manual/reference/operator/query/polygon.md#mongodb-query-op.-polygon)

- [`$center`](https://www.mongodb.com/docs/manual/reference/operator/query/center.md#mongodb-query-op.-center) (defines a circle), and

- [`$centerSphere`](https://www.mongodb.com/docs/manual/reference/operator/query/centersphere.md#mongodb-query-op.-centerSphere) (defines a circle on a sphere).

**Important:**

If you use longitude and latitude, specify coordinates in order of `longitude, latitude`.

## Behavior

### Geospatial Indexes

[`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) does not require a geospatial index. However, a geospatial index will improve query performance. Both [2dsphere](https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2dsphere.md#std-label-2dsphere-index) and [2d](https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2d.md#std-label-2d-index) geospatial indexes support [`$geoWithin`.](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin)

### Unsorted Results

The [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) operator does not return sorted results. As such, MongoDB can return [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) queries more quickly than geospatial [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) or [`$nearSphere`](https://www.mongodb.com/docs/manual/reference/operator/query/nearsphere.md#mongodb-query-op.-nearSphere) queries, which sort results.

### Degenerate Geometry

[`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) does not guarantee that it will consider a piece of geometry to contain its component geometry, or another polygon sharing its component geometry.

### "Big" Polygons

For [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin), if you specify a single-ringed polygon that has an area greater than a single hemisphere, include the custom MongoDB coordinate reference system in the [`$geometry`](https://www.mongodb.com/docs/manual/reference/operator/query/geometry.md#mongodb-query-op.-geometry) expression. Otherwise, [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) queries for the complementary geometry. For all other GeoJSON polygons with areas greater than a hemisphere, [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) queries for the complementary geometry.

## Examples

### Within a Polygon

The following example selects all `loc` data that exist entirely within a GeoJSON [`Polygon`](https://www.mongodb.com/docs/manual/reference/geojson.md#std-label-geojson-polygon). The area of the polygon is less than the area of a single hemisphere:

```javascript
db.places.find(
   {
     loc: {
       $geoWithin: {
          $geometry: {
             type : "Polygon" ,
             coordinates: [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ]
          }
       }
     }
   }
)
```

For single-ringed polygons with areas greater than a single hemisphere, see [Within a "Big" Polygon.](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#std-label-geowithin-big-polygon)

### Within a "Big" Polygon

To query with a single-ringed GeoJSON polygon whose area is greater than a single hemisphere, the [`$geometry`](https://www.mongodb.com/docs/manual/reference/operator/query/geometry.md#mongodb-query-op.-geometry) expression must specify the custom MongoDB coordinate reference system. For example:

```javascript
db.places.find(
   {
     loc: {
       $geoWithin: {
          $geometry: {
             type : "Polygon" ,
             coordinates: [
               [
                 [ -100, 60 ], [ -100, 0 ], [ -100, -60 ], [ 100, -60 ], [ 100, 60 ], [ -100, 60 ]
               ]
             ],
             crs: {
                type: "name",
                properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
             }
          }
       }
     }
   }
)
```
