MongoDB Indexes (part 1)

In this article we will deal with one of the more forgotten and most powerful methods to optimize our MongoDB database. Yes, we are talking about the indexes.

Without the existence of indexes, MongoDB would have to scan all documents in our collections on each received query.
To avoid this, MongoDB has several types of indexes that cover all our indexing needs.

Broadly, the indexes in MongoDB work the same way that in other database engines and are basically a type of data structure that stores a portion of the overall data of each collection, storing the value of one or more fields. Thanks to the Indexes, when a query is launched on MongoDB, it examines whether there is an index that matches the search parameters and will access data much more faster.

Sorted Results: MongoDB can use indexes to return documents sorted by the index key directly from the index without requiring an additional sort phase.

Covered Results: When the query criteria and the projection of a query include only the indexed fields, MongoDB will return results directly from the index without scanning any documents or bringing documents into memory. These covered queries can be very efficient. Indexes can also cover aggregation pipeline operations.

There are several indexes types:

Default indexes:

Every collections has a an index on the _id field by default

Single field indexes:

Besides Default Indexes on the _id field, MongoDB also supports user-defined indexes. Single field indexes are those who are applied to a single field of our document.

Indexes can be ascending (1) or descending (-1)

For example, to create an ascending index for the orderDate field on our orders collections we should do the next:

We must never forget that indexes requires some disk space to be stored so, before execute an index operation, make sure that you have the minimun free size, taking in mind the size of your collections.

Compound Indexes:

MongoDB also supports user-defined indexes for multiple fields at once.

Lets imagine the users collections with two relevant fields (userid & score) and we want to perform a search for the two fields simultaneously.

In order to do this in a fast way, we should perform a new index the next way:

Order matters: As the example above, MongoDB Engine will order first by the userid field and, once done, will take place the order by the score field.

Multikey Indexes:

Finally, MongoDB can create indexes on the elements stored in arrays. This process is done automatically when creating an index. MongoDB determine if the field on which the Index is released is an array and create an index for each of the array elements.

We can see a practical example imagining again the previous collection somewhat more complex:

… and applying an index on field street of the address array:

We finished with this short introduction to the indexes, only talking about their types and methods of creation.
In the next parts we’ll talk about other types of indexes (geographic, text and hash) and show a practical example of the performance increases that they provide.

Don’t forget to take a look to the full MongoDB Indexes documentation.

Continue reading MongoDB Indexes part 2 about Geospatial, Text and Hash Indexes and other Index properties

César Trigo

Backend Development Director @ Gigigo Group

You may also like...

Leave a Reply