Guided Index Creation for Mongo Entities

Available from Release 5.2.0 of Onesait Platform

Indexes in Mongo

Creating indexes is a fundamental part of query design and optimization in Mongo, indexes improve query efficiency by allowing MongoDB to quickly access documents that match certain search criteria without having to scan the entire collection.

Indexes are based on one or more fields of the documents in a collection and are used to speed up queries involving those fields.

In MongoDB, there are several types of indexes that can be created depending on the specific needs of your application. The two main types of index creation are single indexes and multiple or compound indexes.

Simple Index

A simple index is set up with a single property. It can be ascending or descending, which determines how the values are organized within the index.

These indexes are valuable when you are looking to optimize the search or classification of documents based on a single characteristic. A practical case would be the creation of a simple index on the "name" field to speed up the search for documents based on their names.

Multiple or Composite Index

A composite index is created on multiple fields in a document. You can specify the order (ascending or descending) for each field in the composite index, it also shows the order of insertion of properties when creating the index, as it influences and can have a significant impact on query performance and efficiency of search operations.

This is useful when you want to speed up queries that involve several fields in combination. For example, if you want to search for documents by "name" and "id", you can create a composite index on both fields.

Options when creating an index

The options we have to create an index are the following:

  • Background: If we check the check, MongoDB will create the index in the background, which means that it will not block writing to the collection while the index is being created. It is useful for collections with a large amount of data or documents.

  • TTL (Time-To-Live): You can set an index with this option to enable automatic expiration of documents after a specified period of time. This is useful for data that should be automatically deleted after a certain time, by checking the TTL check, a field will automatically appear below to enter the time in seconds. (This option can only be used in Simple indexes).

  • Unique: If checked, MongoDB will ensure that there are no duplicates in the indexed fields. That is, it will not allow more than one document with the same value for the indexed fields. This is useful when you need to guarantee the uniqueness of the data.

    Sparse: If checked, the index will only include documents that contain the indexed field. This is useful when not all documents in the collection have the field you want to index.

Creating indexes from the Control Panel

The Index Creation section can be accessed once the Entity has been created in the Index Configuration tab:

When accessing the screen we can see the list of created indexes, and below the options for the creation of indexes, in which we can enter the name we want to give to our index, the type (Simple or Multiple) and its different options:

  • In simple index (Unique, Sparse , Background and TTL)

  • In multiple index (Unique, Sparse , Background)

Creating a simple index

To create a simple index, the first step involves assigning a name to the index we wish to create. By default, the index type is set to simple. Next, we select the options we wish to configure for our index and choose the property that will serve as the basis for the creation of the index.

It is important to note that it is not possible to create unique indexes using properties of type "Object" or "Array", since these options are disabled.

Finally, we conclude the process by clicking on the "Create index" option.

Example of simple index creation with options:

Example of simple incide creation without options, descending;

Example of index creation with Background and Unique;

We can see how it appears in the list at the top, specifying the added options.

Creating Multiple or Composite Indexes

To create a composite index, the process starts by assigning a name to the composite index we want to create. Then, we modify the index type from "simple" to "multiple". Next, we select the options we want to configure for our index and specify the properties that will serve as the basis for the creation of the composite index.

It is possible to observe the order in which these properties are arranged when creating the index in the "Field Order" column. It is important to remember that it is not feasible to create compound indexes using properties of type "Object" or "Array", since these options are disabled. Finally, we confirm the creation of the composite index.

Example of a composite index with options, with "name" ascending and the others descending:

Example of a composite index without options;

Example of a composite index with Sparse and all sorted in descending order:

Podemos ver como aparece en el listado de la parte superior, especificando las opciones añadidas, junto a todos los índices creados:

Deleting indexes

To delete the created or existing index, we can do the following when editing the entity

Next, click on "Index Configuration" and delete a simple index and a composite index:

By deleting them we can see how they are removed from the list.

Viewing created indexes

When creating indexes, they can be viewed from three places:

  • From the entity edition in which they can be created and deleted.

  • From the entity visualization, in which only the indexes can be visualized.

  • From the Query Tool, in which we can create, delete and visualize.

Query Tool

By executing the getIndex() statement; we can obtain the indexes:

By executing dropIndex(), we delete the index

We can see how it has been removed:

With createIndex() we can create the index:

We can observe how it has been created:

Â