• Indexes >
  • Geospatial Indexes and Queries

Geospatial Indexes and Queries

MongoDB offers a number of indexes and query mechanisms to handle geospatial information. This section introduces MongoDB’s geospatial features.

Surfaces

Before storing your location data and writing queries, you must decide the type of surface to use to perform calculations. The type you choose affects how you store data, what type of index to build, and the syntax of your queries.

MongoDB offers two surface types:

  • Spherical

    To calculate geometry over an Earth-like sphere, store your location data on a spherical surface and use 2dsphere index.

    Store your location data as GeoJSON objects with this coordinate-axis order: longitude, latitude. The coordinate reference system for GeoJSON uses the WGS84 datum.

  • Flat

    To calculate distances on a Euclidean plane, store your location data as legacy coordinate pairs and use a 2d index.

Location Data

If you choose spherical surface calculations, you store location data as

  • GeoJSON objects (preferred).

    Queries on GeoJSON objects always calculate on a sphere. The default coordinate reference system for GeoJSON uses the WGS84 datum.

    New in version 2.4: The storage and querying of GeoJSON objects is new in version 2.4. Prior to version 2.4, all geospatial data was stored as coordinate pairs.

    MongoDB supports the following GeoJSON objects:

    • Point
    • LineString
    • Polygon
  • Legacy coordinate pairs

    MongoDB supports spherical surface calculations on legacy coordinate pairs by converting the data to the GeoJSON Point type.

If you choose flat surface calculations, you can store data only as legacy coordinate pairs.

Query Operations

MongoDB’s geospatial query operators let you query for:

  • Inclusion. MongoDB can query for locations contained entirely within a specified polygon. Inclusion queries use the $geoWithin operator.
  • 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.
  • Proximity. MongoDB can query for the points nearest to another point. Proximity queries use the $near operator. The $near operator requires a 2d or 2dsphere index.

Geospatial Indexes

MongoDB provides the following geospatial index types to support the geospatial queries:

  • 2dsphere, which supports:

    • Calculations on a sphere
    • Both GeoJSON objects and legacy coordinate pairs
    • A compound index with scalar index fields (i.e. ascending or descending) as a prefix or suffix of the 2dsphere index field

    New in version 2.4: 2dsphere indexes are not available before version 2.4.

  • 2d, which supports:

    • Calculations using flat geometry
    • Legacy coordinate pairs (i.e., geospatial points on a flat coordinate system)
    • A compound index with only one additional field, as a suffix of the 2d index field

Geospatial Indexes and Sharding

You cannot use a geospatial index as the shard key index.

You can create and maintain a geospatial index on a sharded collection if using different fields as the shard key.

Queries using $near are not supported for sharded collections. Use geoNear instead. You also can query for geospatial data using $geoWithin.

Additional Resources

The following pages provide complete documentation for geospatial indexes and queries: