---
title: "Make a Spatial Query"
url: "https://docs.ravendb.net/6.2/client-api/session/querying/how-to-make-a-spatial-query"
version: "6.2"
language: "nodejs"
---

# Make a Spatial Query

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-make-a-spatial-query.md), [Java](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-make-a-spatial-query.java.md), [Python](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-make-a-spatial-query.python.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - Documents that contain spatial data can be queried by spatial queries that employ geographical criteria.  
>   You can use either *Dynamic spatial query* or *Spatial index query*.
>
>   - **Dynamic spatial query**  
>     Make a dynamic spatial query on a collection (described below).  
>     An auto-index will be created by the server.
>
>   - **Spatial index query**  
>     Index your documents' spatial data in a static-index (see [indexing spatial data](https://docs.ravendb.net/6.2/indexes/indexing-spatial-data.nodejs.md))
>     and then make a spatial query on this index (see [query a spatial index](https://docs.ravendb.net/6.2/indexes/querying/spatial.nodejs.md)).
>
> - To perform a spatial search,  
>   use the `spatial` method, which provides a wide range of spatial functionalities.
>
> - When making a dynamic spatial query from Studio,  
>   results are also displayed on the global map. See [spatial queries map view](https://docs.ravendb.net/6.2/studio/database/queries/spatial-queries-map-view.md).
>
> - In this page:
>
>   - [Search by radius](#search-by-radius)
>   - [Search by shape](#search-by-shape)
>     - [Circle](#circle)
>     - [Polygon](#polygon)
>   - [Spatial sorting](#spatial-sorting)
>     - [Order by distance](#orderbydistance)
>     - [Order by distance desc](#orderbydistancedesc)
>     - [Rounded distance](#roundeddistance)
>     - [Get resulting distance](#getresultingdistance)
>   - [Spatial API](#spatial-api)

## Search by radius

Use the `withinRadius` method to search for all documents containing spatial data that is located  
within the specified distance from the given center point.

### Query

```javascript
// This query will return all matching employee entities
// that are located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

// Define a dynamic query on 'employees' collection
const employeesWithinRadius = await session
  .query({ collection: "employees" })
   // Call 'spatial' method
  .spatial(
      // Specify the  document fields containing the spatial data
      new PointField("address.location.latitude", "address.location.longitude"),
      // Set the geographical area in which to search for matching documents
      // Call 'withinRadius', pass the radius and the center points coordinates  
      criteria => criteria.withinRadius(20, 47.623473, -122.3060097))
  .all();
```

### RQL

```sql
// This query will return all matching employee entities
// that are located within 20 kilometers radius
// from point (47.623473 latitude, -122.3060097 longitude).

from "employees"
where spatial.within(
  spatial.point(address.location.latitude, address.location.longitude),
  spatial.circle(20, 47.623473, -122.3060097)
)
```

## Search by shape

- Use the `relatesToShape` method to search for all documents containing spatial data that is located  
  in the specified relation to the given shape.

- The shape is specified as either a **circle** or a **polygon** in a [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry) format.

- The relation to the shape can be one of: `Within`, `Contains`, `Disjoint`, `Intersects`.

> **Note**
>
> &#x20;**Circle**:
>
> ### Query
>
> ```javascript
> // This query will return all matching employee entities
> // that are located within 20 kilometers radius
> // from point (47.623473 latitude, -122.3060097 longitude).
>
> // Define a dynamic query on 'employees' collection
> const employeesWithinShape = await session
>   .query({ collection: "employees" })
>    // Call 'spatial' method
>   .spatial(
>       // Specify the  document fields containing the spatial data
>       new PointField("address.location.latitude", "address.location.longitude"),
>       // Set the geographical search criteria, call 'relatesToShape'
>       criteria => criteria.relatesToShape(
>           // Specify the WKT string. Note: longitude is written FIRST
>           "CIRCLE(-122.3060097 47.623473 d=20)",
>           // Specify the relation between the WKT shape and the documents spatial data
>           "Within",
>           // Customize radius units (default is Kilometers) and error percentage (Optional)
>           "Miles",
>           0))
>   .all();
> ```
>
> ### RQL
>
> ```sql
> // This query will return all matching employee entities
> // that are located within 20 kilometers radius
> // from point (47.623473 latitude, -122.3060097 longitude).
>
> from "employees"
> where spatial.within(
>   spatial.point(address.location.latitude, address.location.longitude),
>   spatial.wkt("CIRCLE(-122.3060097 47.623473 d=20)", "miles")
> )
> ```

> **Note**
>
> &#x20;**Polygon**:
>
> ### Query
>
> ```javascript
> // This query will return all matching company entities
> // that are located within the specified polygon.
>
> // Define a dynamic query on 'companies' collection
> const companiesWithinShape = await session
>   .query({ collection: "employees" })
>   // Call 'spatial' method
>   .spatial(
>       // Specify the  document fields containing the spatial data
>       new PointField("address.location.latitude", "address.location.longitude"),
>       // Set the geographical search criteria, call 'relatesToShape'
>       criteria => criteria.relatesToShape(
>           // Specify the WKT string
>           `POLYGON ((
>               -118.6527948 32.7114894,
>               -95.8040242 37.5929338,
>               -102.8344151 53.3349629,
>               -127.5286633 48.3485664,
>               -129.4620208 38.0786067,
>               -118.7406746 32.7853769,
>               -118.6527948 32.7114894
>           ))`,
>           // Specify the relation between the WKT shape and the documents spatial data
>           "Within"))
>   .all();
> ```
>
> ### RQL
>
> ```sql
> // This query will return all matching company entities
> // that are located within the specified polygon.
>
> from "companies"
> where spatial.within(
>   spatial.point(address.location.latitude, address.location.longitude),
>   spatial.wkt("POLYGON ((
>       -118.6527948 32.7114894,
>       -95.8040242 37.5929338,
>       -102.8344151 53.3349629,
>       -127.5286633 48.3485664,
>       -129.4620208 38.0786067,
>       -118.7406746 32.7853769,
>       -118.6527948 32.7114894))")
> )
> ```
>
> > **Info**
> >
> > &#x20;**Polygon rules**:
> >
> > - The polygon's coordinates must be provided in counterclockwise order.
> >
> > - The first and last coordinates must mark the same location to form a closed region.
> >
> > ![WKT polygon](https://docs.ravendb.net/assets/images/spatial_1-779c509ab611ef1c93896d4b50a1790d.png)

## Spatial sorting

- Use `orderByDistance` or `orderByDistanceDescending` to sort the results by distance from a given point.

- By default, distance in RavenDB measured in **kilometers**.  
  The distance can be rounded to a specific range.

> **Note**
>
> &#x20;**Order by distance**:
>
> ### Query
>
> ```javascript
> // Return all matching employee entities located within 20 kilometers radius
> // from point (47.623473 latitude, -122.3060097 longitude).
>
> // Sort the results by their distance from a specified point,
> // the closest results will be listed first.
>
> const employeesSortedByDistance = await session
>   .query({ collection: "employees" })
>    // Provide the query criteria:
>   .spatial(
>       new PointField("address.location.latitude", "address.location.longitude"),
>       criteria => criteria.withinRadius(20, 47.623473, -122.3060097))
>    // Call 'orderByDistance'
>   .orderByDistance(
>       // Specify the document fields containing the spatial data
>       new PointField("address.location.latitude", "address.location.longitude"),
>       // Sort the results by their distance from this point: 
>       47.623473, -122.3060097)
>   .all();
> ```
>
> ### RQL
>
> ```sql
> // Return all matching employee entities located within 20 kilometers radius
> // from point (47.623473 latitude, -122.3060097 longitude).
>
> // Sort the results by their distance from a specified point,
> // the closest results will be listed first.
>
> from "employees"
> where spatial.within(
>   spatial.point(address.location.latitude, address.location.longitude),
>   spatial.circle(20, 47.623473, -122.3060097)
> )
> order by spatial.distance(
>   spatial.point(address.location.latitude, address.location.longitude),
>   spatial.point(47.623473, -122.3060097)
> )
> ```

> **Note**
>
> &#x20;**Order by distance descending**:
>
> ### Query
>
> ```javascript
> // Return all employee entities sorted by their distance from a specified point.
> // The farthest results will be listed first.
>
> const employeesSortedByDistanceDesc = await session
>   .query({ collection: "employees" })
>    // Call 'orderByDistanceDescending'
>   .orderByDistanceDescending(
>       // Specify the document fields containing the spatial data
>       new PointField("address.location.latitude", "address.location.longitude"),
>       // Sort the results by their distance (descending) from this point: 
>       47.623473, -122.3060097)
>   .all();
> ```
>
> ### RQL
>
> ```sql
> // Return all employee entities sorted by their distance from a specified point.
> // The farthest results will be listed first.
>
> from "employees"
> order by spatial.distance(
>   spatial.point(address.location.latitude, address.location.longitude),
>   spatial.point(47.623473, -122.3060097)
> ) desc
> ```

> **Note**
>
> &#x20;**Sort results by rounded distance**:
>
> ### Query
>
> ```javascript
> // Return all employee entities.
> // Results are sorted by their distance to a specified point rounded to the nearest 100 km interval.
> // A secondary sort can be applied within the 100 km range, e.g. by field lastName.
>
> const employeesSortedByRoundedDistance = await session
>   .query({ collection: "employees" })
>    // Call 'orderByDistanceDescending'
>   .orderByDistance(
>       // Specify the document fields containing the spatial data
>       new PointField("address.location.latitude", "address.location.longitude")
>            // Round up distance to 100 km 
>           .roundTo(100),
>       // Sort the results by their distance (descending) from this point: 
>       47.623473, -122.3060097)
>    // A secondary sort can be applied
>   .orderBy("lastName")
>   .all();
> ```
>
> ### RQL
>
> ```sql
> // Return all employee entities.
> // Results are sorted by their distance to a specified point rounded to the nearest 100 km interval.
> // A secondary sort can be applied within the 100 km range, e.g. by field lastName.
>
> from "employees"
> order by spatial.distance(
>   spatial.point(address.location.latitude, address.location.longitude),
>   spatial.point(47.623473, -122.3060097),
>   100
> ), lastName
> ```

> **Note**
>
> &#x20;**Get resulting distance**:
>
> - The distance is available in the `@spatial` metadata property within each result.
>
> - Note the following difference between the underlying search engines:
>
>   - When using **Lucene**:  
>     This metadata property is always available in the results.
>
>   - When using **Corax**:  
>     In order to enhance performance, this property is not included in the results by default.  
>     To get this metadata property you must set the [Indexing.Corax.IncludeSpatialDistance](https://docs.ravendb.net/6.2/server/configuration/indexing-configuration.md#indexingcoraxincludespatialdistance) configuration value to *true*.  
>     Learn about the available methods for setting an indexing configuration key in this [indexing-configuration](https://docs.ravendb.net/6.2/server/configuration/indexing-configuration.md) article.
>
> ```javascript
> // Get the distance of the results:
> // ================================
>
> // Call 'GetMetadataFor', pass an entity from the resulting employees list
> const metadata = session.advanced.getMetadataFor(employeesSortedByDistance[0]);
>
> // The distance is available in the '@spatial' metadata property
> const spatialResults = metadata["@spatial"];
>
> const distance = spatialResults.Distance;   // The distance of the entity from the queried location
> const latitude = spatialResults.Latitude;   // The entity's longitude value
> const longitude = spatialResults.Longitude; // The entity's longitude value
> ```

## Spatial API

> **Info**
>
> #### spatial

```javascript
spatial(fieldName, clause);
spatial(field, clause);
```

| Parameters | Type | Description |
| - | - | - |
| **fieldName** | `string` | Path to spatial field in an index<br />(when querying an index). |
| **field** | `DynamicSpatialField` | Object that contains the document's spatial fields,<br />either `PointField` or `WktField`<br />(when making a dynamic query). |
| **clause** | `(SpatialCriteriaFactory) => SpatialCrieteria` | Spatial criteria that will be executed on a given spatial field. |

> **Info**
>
> #### DynamicSpatialField

```javascript
class PointField {
  latitude; 
  longitude;
}

class WktField {
  wkt;
}
```

| Parameters | Type | Description |
| - | - | - |
| **latitude** | `string` | Path to the document field that contains the latitude |
| **longitude** | `string` | Path to the document field that contains the longitude |
| **wktPath** | `string` | Path to the document field that contains the WKT string |

> **Info**
>
> #### SpatialCriteriaFactory

```javascript
relatesToShape(shapeWkt, relation);
relatesToShape(shapeWkt, relation, units, distErrorPercent);
intersects(shapeWkt);
intersects(shapeWkt, distErrorPercent);
intersects(shapeWkt, distErrorPercent);
intersects(shapeWkt, units, distErrorPercent);
contains(shapeWkt);
contains(shapeWkt, units);
contains(shapeWkt, distErrorPercent);
contains(shapeWkt, units, distErrorPercent);
disjoint(shapeWkt);
disjoint(shapeWkt, units);
disjoint(shapeWkt, distErrorPercent);
disjoint(shapeWkt, units, distErrorPercent);
within(shapeWkt);
within(shapeWkt, units);
within(shapeWkt, distErrorPercent);
within(shapeWkt, units, distErrorPercent);
withinRadius(radius, latitude, longitude);
withinRadius(radius, latitude, longitude, radiusUnits);
withinRadius(radius, latitude, longitude, radiusUnits, distErrorPercent);
```

| Parameter | Type | Description |
| - | - | - |
| **shapeWkt** | `string` | [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry)-based shape used in query criteria |
| **relation** | `string` | Relation of the shape to the spatial data in the document/index.<br />Can be `Within`, `Contains`, `Disjoint`, `Intersects`. |
| **distErrorPercent** | `number` | Maximum distance error tolerance in percents. Default: 0.025 |
| **radius** / **latitude** / **longitude** | `number` | Used to define a radius of a circle |
| **radiusUnits** / **units** | `string` | Determines if circle or shape should be calculated in `Kilometers` or `Miles`.<br />By default, distances are measured in kilometers. |

> **Info**
>
> #### orderByDistance

```javascript
orderByDistance(field, latitude, longitude);
orderByDistance(field, shapeWkt);
orderByDistance(fieldName, latitude, longitude);
orderByDistance(fieldName, latitude, longitude, roundFactor: number);
orderByDistance(fieldName, shapeWkt);
```

> **Info**
>
> #### orderByDistanceDescending

```javascript
orderByDistanceDescending(field, latitude, longitude);
orderByDistanceDescending(field, shapeWkt);
orderByDistanceDescending(fieldName, latitude, longitude);
orderByDistanceDescending(fieldName, latitude, longitude, roundFactor);
orderByDistanceDescending(fieldName, shapeWkt);
```

| Parameter | Type | Description |
| - | - | - |
| **fieldName** | `string` | Path to spatial field in index<br />(when querying an index). |
| **field** | `DynamicSpatialField` | Object that contains the document's spatial fields,<br />either `PointField` or `WktField`<br />(when making a dynamic query). |
| **shapeWkt** | `string` | [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry)-based shape to be used as a point from which distance will be measured. If the shape is not a single point, then the center of the shape will be used as a reference. |
| **latitude** / **longitude** | `number` | Used to define a point from which distance will be measured |
| **roundFactor** | `number` | A distance interval in kilometers.<br />The distance from the point is rounded up to the nearest interval.<br />The results within the same interval can be sorted by a secondary order.<br />If no other order was specified, then by ascending order of document Id. |

## See also

- [How to Query](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.nodejs.md)
- [Querying: Basics](https://docs.ravendb.net/6.2/indexes/querying/query-index.nodejs.md)
- [Indexing Spatial Data](https://docs.ravendb.net/6.2/indexes/indexing-spatial-data.nodejs.md)
- [Query spatial index](https://docs.ravendb.net/6.2/indexes/querying/spatial.nodejs.md)
- [Spatial query view](https://docs.ravendb.net/6.2/studio/database/queries/spatial-queries-map-view.md)
