Navigation
This version of the documentation is archived and no longer supported.

Create a 2dsphere Index

On this page

To create a geospatial index for GeoJSON-formatted data, use the db.collection.ensureIndex() method to create a 2dsphere index. In the index specification document for the db.collection.ensureIndex() method, specify the location field as the index key and specify the string literal "2dsphere" as the value:

db.collection.ensureIndex( { <location field> : "2dsphere" } )

The following procedure presents steps to populate a collection with documents that contain a GeoJSON data field and create 2dsphere indexes. Although the procedure populates the collection first, you can also create the indexes before populating the collection.

Procedure

First, populate a collection places with documents that store location data as GeoJSON Point in a field named loc. The coordinate order is longitude, then latitude.

db.places.insert(
   {
      loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },
      name: "Central Park",
      category : "Parks"
   }
)

db.places.insert(
   {
      loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },
      name: "La Guardia Airport",
      category : "Airport"
   }
)

Then, create the 2dsphere index.

Create a 2dsphere Index

For example, the following creates a 2dsphere index on the location field loc:

db.places.ensureIndex( { loc : "2dsphere" } )

Create a Compound Index with 2dsphere Index Key

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

db.places.ensureIndex( { loc : "2dsphere" , category : -1, name: 1 } )

Unlike the 2d index, a compound 2dsphere index does not require the location field to be the first field indexed. For example:

db.places.ensureIndex( { category : 1 , loc : "2dsphere" } )

Considerations

Fields with 2dsphere indexes must hold geometry data in the form of 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.

The geoNear command and the $geoNear pipeline stage require that a collection have at most only one 2dsphere index and/or only one 2d index whereas geospatial query operators (e.g. $near and $geoWithin) permit collections to have multiple geospatial indexes.

The geospatial index restriction for the geoNear command and the $geoNear pipeline stage exists because neither the geoNear command nor the $geoNear pipeline stage syntax includes the location field. As such, index selection among multiple 2d indexes or 2dsphere indexes is ambiguous.

No such restriction applies for geospatial query operators since these operators take a location field, eliminating the ambiguity.

As such, although this tutorial creates multiple 2dsphere indexes, to use the geoNear command or the $geoNear pipeline stage against the example collection, you will need to drop all but one of the 2dsphere indexes.

To query using the 2dsphere index, see Query a 2dsphere Index.