MongoDB Indexes (part 2) – Geospatial, Text and Hash

Following the introduction of the basic types of indexes in MongoDB, in this article we will dig a little deeper talking about other less common types of indexes: Geospatial Index, Text Index, Hash Index and other index properties: Unique and Sparse.

Geospatial Indexes

To support efficient queries to the database involving geographical coordinates, MongoDB has two types of indexes, 2d and 2dsphere.

MongoDB allows only one geographical index by collection and also , it can never be a primary index on sharded environments.

It is important to remember that Geospatial Indexes, like those seen in part 1, can be Composite Indexes.

2d indexes

Used to index points in a two-dimensional plane .
2d indexes support calculations in a Euclidean plane (flat surface)

You must use indexes if you are not going to store data in GeoJSON format. In case you stores data structures based on GeoJSON you must use 2dsphere indexes. We will talk about it below.

To store locations as pairs of coordinates, it is recommended to use simple Arrays to avoid those languages ​​that do not support associative arrays:

To create a 2d index over the field loc we can do the next:

2dsphere Indexes

2dsphere indexes are a type of indexes that support queries that calculate geometries on an earth-like sphere. 2dsphere indexes are are only available from MongoDB 2.4.

You must use spherical indices if you intend to store data in GeoJSON structures

2dsphere Indexes supports the following GeoJSON objects:

  • Point
  • LineString
  • Polygon

To store the geoloaction data, create a new field that contains one of the GeoJSON supported objects as follows:

To create a 2dsphere index use the following statement:

Text indexes

MongoDB has a certain type of indexes to support text searchs. These indexes excluded from the search certain prepositions and conjunctions by language.

To create a Text Index we’ll use the following statement:

The supported languages at this moment are:

  • danish
  • dutch
  • english
  • finnish
  • french
  • german
  • hungarian
  • italian
  • norwegian
  • portuguese
  • romanian
  • russian
  • spanish
  • swedish
  • turkish

Hash Indexes

To support hash based sharding envitonments, MongoDB has the Hash Index type that helps to index the hash of the value of a field. Ensuring a ramdom distribution of values along their range across the sharded collections.


Unique Indexes:

Sometimes we will need unique fields across our enteir collection and can never be duplicated to avoid, for example, integrity issues. To solve this issue MongoDB allows to add the unique  option to the ensureIndex statement when created, making the target field unique across the entire collection.

Sparse Indexes:

Also, to ensure maximum performance of the indexes, we may want to omit from indexing those documents NOT containing the field on which you are performing an index. To do this we use the sparse property:

And this concludes the brief introduction to the MongoDB Indexes , leaving pending a deeper immersion on geolocation aspects to be discussed in successive deliveries.

César Trigo

Backend Development Director @ Gigigo Group

Leave a Reply