MongoDB allows for geospatial queries on location data stored in collections. There are two main index types - 2d for flat geometry and 2dsphere for spherical geometry. Queries include proximity queries to find nearest points, intersection queries to find intersections with shapes, and inclusion queries to find points within shapes. The appropriate index type and query operators depend on whether location data is represented as legacy coordinate pairs or GeoJSON objects and whether distances are calculated on a flat plane or sphere.
6. Spherical
To calculate geometry over an Earth-like
sphere, store your location data on a spherical
surface and use 2dsphere index.
MongoDB Reference
7. Store your location data as GeoJSON objects
with this coordinate-axis order: longitude,
latitude. The coordinate reference system for
GeoJSON uses the WGS84 datum.
MongoDB Reference
13. Inclusion
MongoDB can query for locations contained
entirely within a specified polygon. Inclusion
queries use the $geoWithin operator. No index
required, 2d and 2dsphere supports
MongoDB Reference
14. Intersection
MongoDB can query for locations that intersect
with a specified geometry. These queries apply
only to data on a spherical surface. These
queries use the $geoIntersects operator. Index
required, only 2dsphere supports
MongoDB Reference
15. Proximity
MongoDB can query for the points nearest to
another point. Proximity queries use the $near
operator. 2d or 2dsphere required
MongoDB Reference
17. 2d
● Calculations using flat geometry
● Legacy coordinate pairs (i.e., geospatial
points on a flat coordinate system)
● Compound indexes with only one additional
field, as a suffix of the 2d index field
18. 2d Index Creation
db.collection.createIndex( { <location field> : "2d" } ,
{ min : <lower bound> , max : <upper bound> } )
db.collection.createIndex( { loc : "2d" } ,
{ min : -45 , max : 45 } )
25. 2dsphere
● Calculations on a sphere
● GeoJSON objects and include backwards
compatibility for legacy coordinate pairs
● Compound indexes with scalar index fields
(i.e. ascending or descending) as a prefix or
26. geoHaystack
An index is a special index that is optimized to
return results over small areas. geoHaystack
indexes improve performance on queries that
use flat geometry.