> 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

# $near (query predicate operator)

## Definition

Specifies a point for which a [geospatial](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-geospatial) query returns the documents from nearest to farthest. The [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) operator can specify either a [GeoJSON](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-GeoJSON) point or legacy coordinate point.

[`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) requires a geospatial index:

- [2dsphere](https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2dsphere.md#std-label-2dsphere-index) index if specifying a [GeoJSON](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-GeoJSON) point.

- [2d](https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2d.md#std-label-2d-index) index if specifying a point using legacy coordinates.

To specify a [GeoJSON](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-GeoJSON) point, [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) operator requires a [2dsphere](https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2dsphere.md#std-label-2dsphere-index) index and has the following syntax:

```javascript
{
   <location field>: {
     $near: {
       $geometry: {
          type: "Point" ,
          coordinates: [ <longitude> , <latitude> ]
       },
       $maxDistance: <distance in meters>,
       $minDistance: <distance in meters>
     }
   }
}
```

**Important:**

If specifying latitude and longitude coordinates, list the **longitude** first, and then **latitude**.

- Valid longitude values are between `-180` and `180`, both inclusive.

- Valid latitude values are between `-90` and `90`, both inclusive.

When specifying a [GeoJSON](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-GeoJSON) point, you can use the *optional* [`$minDistance`](https://www.mongodb.com/docs/manual/reference/operator/query/mindistance.md#mongodb-query-op.-minDistance) and [`$maxDistance`](https://www.mongodb.com/docs/manual/reference/operator/query/maxdistance.md#mongodb-query-op.-maxDistance) specifications to limit the [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) results by distance in *meters*:

- [`$minDistance`](https://www.mongodb.com/docs/manual/reference/operator/query/mindistance.md#mongodb-query-op.-minDistance) limits the results to those documents that are *at least* the specified distance from the center point.

- [`$maxDistance`](https://www.mongodb.com/docs/manual/reference/operator/query/maxdistance.md#mongodb-query-op.-maxDistance) limits the results to those documents that are *at most* the specified distance from the center point.

To specify a point using legacy coordinates, [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) requires a [2d](https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2d.md#std-label-2d-index) index and has the following syntax:

```javascript
{
  $near: [ <x>, <y> ],
  $maxDistance: <distance in radians>
}
```

When specifying a legacy coordinate, you can use the *optional* [`$maxDistance`](https://www.mongodb.com/docs/manual/reference/operator/query/maxdistance.md#mongodb-query-op.-maxDistance) specification to limit the [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) results by distance in *radians*. [`$maxDistance`](https://www.mongodb.com/docs/manual/reference/operator/query/maxdistance.md#mongodb-query-op.-maxDistance) limits the results to those documents that are *at most* the specified distance from the center point.

## Behavior

### Special Indexes Restriction

You cannot combine the [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) operator, which requires a special [geospatial index](https://www.mongodb.com/docs/manual/geospatial-queries.md#std-label-index-feature-geospatial), with a query operator or command that requires another special index. For example you cannot combine [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) with the [`$text`](https://www.mongodb.com/docs/manual/reference/operator/query/text.md#mongodb-query-op.-text) query.

### Sort Operation

The [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) operator sorts documents by distance.

- If you use the [`sort()`](https://www.mongodb.com/docs/manual/reference/method/cursor.sort.md#mongodb-method-cursor.sort) method in your query, MongoDB performs a second sort operation, re-ordering the matching documents.  When querying large collections, this can negatively affect query performance.

- If the order of the documents is not important to you, consider using the [`$geoWithin`](https://www.mongodb.com/docs/manual/reference/operator/query/geowithin.md#mongodb-query-op.-geoWithin) operator instead, as it returns unsorted results.

- [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) is a Match Execution operator and is not permitted in aggregation pipelines.

### Validation

Starting in MongoDB 8.0, [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near), [`$nearSphere`](https://www.mongodb.com/docs/manual/reference/operator/query/nearsphere.md#mongodb-query-op.-nearSphere), and [`$geoNear`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/geonear.md#mongodb-pipeline-pipe.-geoNear) validate that the type of the specified [GeoJSON points](https://www.mongodb.com/docs/manual/reference/geojson.md#std-label-geojson-point) is `Point`. Any other input type returns an error.

### Memory Use

Starting in MongoDB 9.0, MongoDB tracks the memory that [`$near`](https://www.mongodb.com/docs/manual/reference/operator/query/near.md#mongodb-query-op.-near) uses to buffer results and to remove duplicate records. If the query exceeds the 100 megabyte memory limit for the stage, MongoDB ends the query and returns an error.

Only queries that match a large number of documents reach the limit. To reduce the memory that the query requires, specify `maxDistance` to restrict the search area. This memory counts toward the per-operation memory limit. To learn more, see [Per-Operation Memory Limit.](https://www.mongodb.com/docs/manual/core/aggregation-pipeline-limits.md#std-label-operation-memory-limit)

## Examples

### Query on GeoJSON Data

**Important:**

If specifying latitude and longitude coordinates, list the **longitude** first, and then **latitude**.

- Valid longitude values are between `-180` and `180`, both inclusive.

- Valid latitude values are between `-90` and `90`, both inclusive.

Consider a collection `places` that has a `2dsphere` index.

The following example returns documents that are at least `1000` meters from and at most `5000` meters from the specified GeoJSON point, sorted from nearest to farthest:

```javascript
db.places.find(
   {
     location:
       { $near :
          {
            $geometry: { type: "Point",  coordinates: [ -73.9667, 40.78 ] },
            $minDistance: 1000,
            $maxDistance: 5000
          }
       }
   }
)
```

### Query on Legacy Coordinates

**Important:**

If specifying latitude and longitude coordinates, list the **longitude** first, and then **latitude**.

- Valid longitude values are between `-180` and `180`, both inclusive.

- Valid latitude values are between `-90` and `90`, both inclusive.

Consider a collection `legacy2d` that has a `2d` index.

The following example returns documents that are at most `0.10` radians from the specified legacy coordinate pair, sorted from nearest to farthest:

```javascript
db.legacy2d.find(
   { location : { $near : [ -73.9667, 40.78 ], $maxDistance: 0.10 } }
)
```
