2dsphere index in MongoDB


What is the 2dsphere index in MongoDB?

A 2dsphere index supports queries that calculate geometries on an earth-like sphere. 2dsphere index supports all MongoDB geospatial queries: queries for inclusion, intersection, and proximity. For more information on geospatial queries

A 2dsphere index supports queries that calculate geometries on an earth-like sphere. 2dsphere index supports all MongoDB geospatial queries: queries for inclusion, intersection, and proximity. For more information on geospatial queries, see Geospatial Queries.

The 2dsphere index supports data stored as GeoJSON objects and legacy coordinate pairs. For legacy coordinate pairs, the index converts the data to GeoJSON Point.

sparse Property

Version 2 and later 2dsphere indexes are always sparse and ignore the sparse option. If a document lacks a 2dsphere index field (or the field is null or an empty array), MongoDB does not add an entry to the index, and MongoDB inserts the document for inserts does not add to the 2dsphere index.

For a compound index that includes a 2dsphere index key along with keys of other types, only the 2dsphere index field determines whether the index references a document.

Earlier versions of MongoDB only support 2dsphere (Version 1) indexes. 2dsphere (Version 1) indexes are not sparse by default and will reject documents with null location fields.

Additional GeoJSON Objects

Version 2 and later 2dsphere indexes include support for additional GeoJSON object: MultiPoint, MultiPolygon, and GeometryColleciton.

Considerations

Starting in MongoDB 4.0, you can specify a key option to the $geoNear pipeline stage to indicate the indexed field path. This allows the $geoNear stage to be used on a collection that has multiple 2dsphere indexes and multiple 2d indexes:

  • If your collection has multiple 2dsphere indexes and multiple 2d indexes, you must use the critical option to specify the indexed field path.
  • If you do not specify the key, you cannot have multiple 2dsphere indexes and multiple 2d indexes since, without the key, index selection among multiple 2d indexes or 2dsphere indexes is ambiguous.

Shard Key Restrictions

When sharding a collection, you cannot use a 2dsphere index as a shard key. However, you can create a geospatial index on a sharded collection using a different field as the shard key.

2dsphere Indexed Field Restrictions

Fields with 2dsphere indexes must hold geometry data in coordinate pairs or GeoJSON data. If you attempt to insert a document with non-geometry data in a 2dsphere indexed field or build a 2dsphere index on a collection where the indexed field has non-geometry data, the operation will fail.

Create a 2dsphere Index

To create a 2dsphere index, use the db.collection.createIndex() method and specify the string literal “2dsphere”.

Create a Compound Index with 2dsphere Index Key

A compound index can include a 2dsphere index key with non-geospatial index keys. For example, the following operation creates a compound index where the first key loc is a 2dsphere index key. The remaining keys category and names are non-geospatial index keys, specifically descending (-1) and ascending (1) keys.