Geospatial
Geospatial refers to anything related to information that has a specific location on Earth. This data, also known as geodata, can include:
- Physical features like mountains, rivers, and oceans
- Man-made structures like buildings, roads, and bridges
- Social and economic data like demographics, traffic patterns, and property values
How to create Geospatial Indexes
How to query on Geospatial
Geospatial Query Operators
MongoDB provides the following geospatial query operators:
| Name | Description |
|---|---|
| $geoIntersects | Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects. |
| $geoWithin | Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin. |
| $near | Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near. |
| $nearSphere | Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere. |
Geospatial Aggregation Stage
MongoDB provides the following geospatial aggregation pipeline stage:
| Stage | Description |
|---|---|
| $geoNear | Returns an ordered stream of documents based on the proximity to a geospatial point. Incorporates the functionality of $match, $sort, and $ limit for geospatial data. The output documents include an additional distance field and can include a location identifier field.$geoNear requires a geospatial index. |
Geospatial Models
MongoDB geospatial queries can interpret geometry on a flat surface or a sphere. 2dsphere indexes support only spherical queries (i.e. queries that interpret geometries on a spherical surface). 2d indexes support flat queries (i.e. queries that interpret geometries on a flat surface) and some spherical queries. While 2d indexes support some spherical queries, the use of 2d indexes for these spherical queries can result in error. If possible, use 2dsphere indexes for spherical queries. The following table lists the geospatial query operators, supported query, used by each geospatial operations:
| Operation | Spherical/Flat Query | Notes |
|---|---|---|
| $near (GeoJSON centroid point in this line and the following line, 2dsphere index) | Spherical | See also the $nearSphere operator, which provides the same functionality when used with GeoJSON and a 2dsphere index. |
| $near (legacy coordinates, 2d index) | Flat | |
| $nearSphere (GeoJSON point, 2dsphere index) | Spherical | Provides the same functionality as $near operation that uses GeoJSON point and a 2dsphere index.For spherical queries, it may be preferable to use $nearSphere which explicitly specifies the spherical queries in the name rather than $near operator. |
| $nearSphere (legacy coordinates, 2d index) | Spherical | Use GeoJSON points instead. |
| $geoWithin : { $geometry: ... } | Spherical | |
| $geoWithin : { $box: ... } | Flat | |
| $geoWithin : { $polygon: ... } | Flat | |
| $geoWithin : { $center: ... } | Flat | |
| $geoWithin : { $centerSphere: ... } | Spherical | |
| $geoIntersects | Spherical | |
| $geoNear aggregation stage (2dsphere index) | Spherical | |
Data API
Casewhere.Runtime.DataObjectExpressions.DSL.FilterBuilder
Support GeoSpatial filters
GeoWithin
Used to find locations within filter
Parameters
field: The name of the locationcoordinates: The coordinates of the location
Result: A FilterBuilder object containing the location
Example:
var filter = FilterBuilder.Create().GeoWithin("Department1",[33,33]).Build();
var query = DataSourceEnumerationQuery.For("Company")
.FilterBy(filter)
var result = ctx.Use<IDataApi>().Enumerate(query);
GeoWithinBox
- Used to find locations within box filter
Parameters
field: The name of the locationlowerLeftX: The lower left of the first coordinateslowerLeftY: The lower left of the second coordinatesupperRightX: The upper right of the first coordinatesupperRightY: The upper right of the second coordinates
Result: A FilterBuilder object containing the location within box filte
Example:
var filter = FilterBuilder.Create().GeoWithinBox("Department1",33,33,22,11).Build();
var query = DataSourceEnumerationQuery.For("Company")
.FilterBy(filter)
var result = ctx.Use<IDataApi>().Enumerate(query);
GeoWithinCenter
- Used to find a location within center filter
Parameters
field: The name of the locationx: The x coordinates of the locationy: The y coordinates of the locationradius: The radius of the location
Result: A FilterBuilder object containing the location within center filter
Example:
var filter = FilterBuilder.Create().GeoWithinCenter("Department1",33,33,22,11).Build();
var query = DataSourceEnumerationQuery.For("Company")
.FilterBy(filter)
var result = ctx.Use<IDataApi>().Enumerate(query);
GeoWithinCenterSphere
- Used to find a location within center sphere filter
Parameters
field: The name of the locationx: The x coordinates of the locationy: The y coordinates of the locationradius: The radius of the location
Result: A FilterBuilder object containing the location within center sphere filter
Example:
var filter = FilterBuilder.Create().GeoWithinCenterSphere("Department1",33,33,22,11).Build();
var query = DataSourceEnumerationQuery.For("Company")
.FilterBy(filter)
var result = ctx.Use<IDataApi>().Enumerate(query);
GeoWithinPolygon
- Used to find a location within polygon filter
Parameters
field: The name of the locationpoints: The coordinates of the location
Result: A FilterBuilder object containing the location within polygon filter
Example:
var filter = FilterBuilder.Create().GeoWithinPolygon("Department1",[22,11]).Build();
var query = DataSourceEnumerationQuery.For("Company")
.FilterBy(filter)
var result = ctx.Use<IDataApi>().Enumerate(query);
Search
Near
- Create a geospatial queries that find documents near a specific location, taking distance constraints into account
Parameters
field: This parameter specifies the name of the field in your documents that contains location data or coordinatesx: The parameter represent the longitudey: The parameter represent the latitudemaxDistance: This parameter specifies the maximum distance within which to find documents. It defaults to 0 if not provided.minDistance: This parameter specifies the minimum distance from the target location. It defaults to 0 if not provided
Result: The query object itself for chaining purpose
Example:
var filter = FilterBuilder.Create().Eq("DepartmentId", ctx.Input.Id).Eq("Active", true).Build();
var query = DataSourceEnumerationQuery.For("Company")
.FilterBy(filter)
.Near("Location",3,4);
.OrderBy("FirstName", true);
var result = ctx.Use<IDataApi>().Enumerate(query);
NearSphere
- Create a geospatial query for finding documents near a specific geographic location, emphasizing spherical calculations.
Parameters
field: This parameter specifies the name of the field in your documents that contains location data or coordinatesx: The parameter represent the longitudey: The parameter represent the latitudemaxDistance: This parameter specifies the maximum distance within which to find documents. It defaults to 0 if not provided.minDistance: This parameter specifies the minimum distance from the target location. It defaults to 0 if not provided
Result: The query object itself for chaining purpose.
Example:
var filter = FilterBuilder.Create().Eq("DepartmentId", ctx.Input.Id).Eq("Active", true).Build();
var query = DataSourceEnumerationQuery.For("Company")
.FilterBy(filter)
.NearSphere("Location",3,4);
.OrderBy("FirstName", true);
var result = ctx.Use<IDataApi>().Enumerate(query);
GeoNear
- Create a geospatial queries that find documents near a specific location, taking distance constraints into account
Parameters
field: The name of the locationvalue: A GeoJsonObject geometry for use with the following geospatial query operatorsmaxDistance: This parameter specifies the maximum distance within which to find documents. It defaults to 0 if not provided.minDistance: This parameter specifies the minimum distance from the target location. It defaults to 0 if not provided
Result: The query object itself for chaining purpose.
Example:
var filter = FilterBuilder.Create().Eq("DepartmentId", ctx.Input.Id).Eq("Active", true).Build();
var query = DataSourceEnumerationQuery.For("Company")
.FilterBy(filter)
.GeoNear("Location",GeoJsonObjectValue,3,4);
.OrderBy("FirstName", true);
var result = ctx.Use<IDataApi>().Enumerate(query);
GeoNearSphere
Create a geospatial query for finding documents near a specific geographic location, emphasizing spherical calculations
Parameters
field: The name of the locationvalue: A GeoJsonObject geometry for use with the following geospatial query operatorsmaxDistance: This parameter specifies the maximum distance within which to find documents. It defaults to 0 if not provided.minDistance: This parameter specifies the minimum distance from the target location. It defaults to 0 if not provided
Result: The query object itself for chaining purpose
Example:
var filter = FilterBuilder.Create().Eq("DepartmentId", ctx.Input.Id).Eq("Active", true).Build();
var query = DataSourceEnumerationQuery.For("Company")
.FilterBy(filter)
.GeoNearSphere("Location",GeoJsonObjectValue,3,4);
.OrderBy("FirstName", true);
var result = ctx.Use<IDataApi>().Enumerate(query);