In this post, we’ll take a look at how to create a MongoDB index and modify existing ones using Studio 3T’s Index Manager.
In Studio 3T, connect to your MongoDB server. You’ll then see the databases and their collections in the left-hand Collections pane.
Simply right-click on the collection you want to create a new index for and choose “Add Index…” from the pop-up menu, as shown below.
This will open the “Add Index” view.
At the top, enter the name of your index (if you leave this field empty, a default index name will be generated for you out of the fields and their index types).
In the first tab, named “Fields”, you can then add the fields that you want indexed and assign to each of them the desired index type.
Click “Add Field(s)…” to select the index fields.
Studio 3T does a quick schema scan over a small set of documents in your collection to present you with a tree of available fields in your collection.
Select the fields for your index from the tree, or, in case the schema scan didn’t cover your desired field, enter the field name directly, and click “Add”.
After you have added your index fields, they will appear with the default index type “ascending”.
You can easily change the index type for each field by double-clicking (or hitting ENTER) in its corresponding index type cell.
Simply choose the desired index type from the drop-down list.
At the bottom of the “Fields” tab, you can select various general index options.
One of those options that you should always carefully consider is the “Create in Background” option.
By default, MongoDB will build indexes in the foreground. This will result in compact index sizes and can take significantly less time than building the index in the background.
However, the huge disadvantage of this approach is that the entire database will be locked for read and write operations while the index is building in the foreground. For any live database, this is a serious consideration.
Therefore, although background-built indexes will usually take longer to build and will initially result in less compact index sizes (they will converge over time towards the compactness of foreground-built indexes, though), we generally recommend setting the background option.
In the “Text Options”, you can set the index options that are specific to text indexes.
For example, click the “Default Language” combo box to choose your desired default language of your text index fields.
In the “Fields Weights” table, you can weigh your text index fields in relation to one another.
All text index fields will automatically be added with a default weight of “1”. To change a field’s weight, just double-click the value (or hit ENTER in the selected cell) and overwrite the weight as you see fit.
You can also manually add weight fields by clicking “Add Weight(s)…”. However, this will usually only be necessary if you have previously selected the “$**” field option to index all text fields.
Analogously, in the “Geo Options” tab, you can set index options specific to the different Geo index types.
Under “Partial Index Options” you can choose to create a partial index, so that it only indexes documents that meet your JSON filter expression.
You also get the chance to validate your JSON before creating the partial index.
Studio 3T also lets you – if you so desire – manually overwrite the actual JSON code that will be sent to the MongoDB server for the index creation.
Under the last tab “Collation“, you can use custom collation by choosing values for the predefined settings.
Once you have configured your index, click “Create Index” and the index will be built on the server.
You may close the index view tab at any point, the index build will continue on the server.
In MongoDB, it is not really possible to modify an existing index. Instead, what one essentially needs to do is drop the original index and re-create it from scratch.
Luckily, Studio 3T makes this process completely transparent for you.
To modify an existing index, expand your collection node in your collection list to see all its indexes.
Select the index you would like to modify, right-click it and choose “Modify Index…” from the pop-up menu.
Studio 3T will then retrieve all information about that index and open the familiar index view with all parameters filled already filled in.
Of course, you can edit the parameters under the other tabs (e.g. Text Options, Geo Options) and also manually overwrite the JSON code again.
To re-create the index with the modifications you have made, simply click “Re-create Index”.
We hope that we’ve convinced you how easy it is to add and modify a MongoDB index using Studio 3T.