Geospatial Queries
MongoDB supports query operations on geospatial data. This section introduces MongoDB’s geospatial features.
In MongoDB, you can store geospatial data asGeoJSONobjects or as.
To calculate geometry over an Earth-like sphere, store your location data asGeoJSON objects.
To specify GeoJSON data, use an embedded document with:
a field named
type
that specifies theanda field named
coordinates
that specifies the object’s coordinates.If specifying latitude and longitude coordinates, list thelongitudefirst and thenlatitude:
- Valid longitude values are between
-180
and180
, both inclusive. - Valid latitude values are between
-90
and90
(both inclusive).
- Valid longitude values are between
For example, to specify aGeoJSON Point:
location
:
{
type
:
"Point"
,
coordinates
:
[
-
73.856077
,
40.848447
]
}
For a list of the GeoJSON objects supported in MongoDB as well as examples, see.
MongoDB geospatial queries on GeoJSON objects calculate on a sphere; MongoDB uses theWGS84reference system for geospatial queries on GeoJSON objects.
Legacy Coordinate Pairs
To calculate distances on a Euclidean plane, store your location data as legacy coordinate pairs and use aindex. MongoDB supports spherical surface calculations on legacy coordinate pairs via a2dsphereindex by converting the data to the GeoJSON Point type.
To specify data as legacy coordinate pairs, you can use either an array (preferred) or an embedded document.
Specify via an array (
Preferred
):
<
field
>
:
[
<
x
>
,
<
y
>
]
If specifying latitude and longitude coordinates, list thelongitudefirst and thenlatitude; i.e.
<
field
>
:
[
<
longitude
>
,
<
latitude
>
]
- Valid longitude values are between
-180
and180
, both inclusive. - Valid latitude values are between
-90
and90
(both inclusive).
Specify via an embedded document:
If specifying latitude and longitude coordinates, the first field, regardless of the field name, must contains thelongitudevalue and the second field, thelatitudevalue ; i.e.
<
field
:
{
<
field1
>
:
<
longitude
>
,
<
field2
>
:
<
latitude
>
}
- Valid longitude values are between
-180
and180
, both inclusive. - Valid latitude values are between
-90
and90
(both inclusive).
To specify legacy coordinate pairs, arrays are preferred over an embedded document as some languages do not guarantee associative map ordering.
MongoDB provides the following geospatial index types to support the geospatial queries.
2dsphere
indexes support queries that calculategeometries on an earth-like sphere.
To create a2dsphere
index, use themethod and specify the string literal"2dsphere"
as the index type:
db
.
collection
.
createIndex
(
{
location
field
>
:
"2dsphere"
}
)
where the<locationfield>
is a field whose value is either aGeoJSON objector a.
For more information on the2dsphere
index, see2dsphere Indexes.
indexes support queries that calculategeometries on a two-dimensional plane. Although the index can supportqueries that calculate on a sphere, if possible, use the2dsphereindex for spherical queries.
To create a2d
index, use themethod, specifying the location field as the key and the string literal"2d"
as the index type:
db
.
collection
.
createIndex
(
{
<
location
field
>
:
"2d"
}
)
where the<locationfield>
is a field whose value is alegacy coordinates pair.
For more information on the2d
index, see.
Geospatial Indexes and Sharded Collections
You cannot use a geospatial index as ashard keywhen sharding a collection. However, you can create a geospatial index on a sharded collection by using a different field as the shard key.
For sharded collections, queries usingand$nearSphere
are not supported. You can instead use either thecommand or the$geoNear
aggregation stage.
You can also query for geospatial data for a sharded cluster usingand$geoIntersect
.
Covered Queries
Ageospatial indexescannot.
For spherical queries, use the2dsphere
index result.
The use of2d
index for spherical queries may lead to incorrect results, such as the use of the2d
index for spherical queries that wrap around the poles.
MongoDB provides the following geospatial query operators:
For more details, including examples, see the individual reference page.
Geospatial Command
MongoDB provides the following geospatial command:
For more details, including examples, seegeoNear
reference page.
Geospatial Aggregation Stage
MongoDB provides the following geospatial:
For more details, including examples, see$geoNear
reference page.
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. While2d
indexes support some spherical queries, the use of2d
indexes for these spherical queries can result in error. If possible, use2dsphere
indexes for spherical queries.
The following table lists the geospatial query operators, supported query, used by each geospatial operations:
Create a collectionplaces
with the following documents:
The following operation creates a2dsphere
index on thelocation
field:
db
places
.
createIndex
(
{
location
:
"2dsphere"
}
)
The following query uses theoperator to return documents that are at least 1000 meters from and at most 5000 meters from the specified GeoJSON point, sorted in order from nearest to farthest:
db
.
places
.
find
(
{
:
{
$near
:
{
$geometry
:
{
type
:
"Point"
,
coordinates
:
[
-
73.9667
,
40.78
]
},
$minDistance
:
1000
,
$maxDistance
:
5000
}
}
}
)
The following operation uses thegeoNear
command to return documents that match the query filter{category:"Parks"}
, sorted in order of nearest to farthest to the specified GeoJSON point:
db
.
runCommand
(
{
geoNear
:
"places"
,
near
:
{
type
:
"Point"
,
coordinates
:
[
-
73.9667
,
40.78
]
},
spherical
:
true
,
query
:
{
category
:
"Parks"
}