In this post, we’ll take a look at how to add a MongoDB index and modify existing ones using Studio 3T.
Download our MongoDB GUI now and let’s get started.
- Add a New Index
- Modify a MongoDB Index
Add a New MongoDB Index
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.
Display Available 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.
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.
Text Index Options
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 you 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.
Geo Index Options
Analogously, in the “Geo Options” tab, you can set index options specific to the different Geo index types.
Partial Index Options
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.
Overwrite JSON Code
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.
Any changes you make here to the JSON code will of course be parsed and automatically set in the other tabs accordingly!
Use Custom Collation
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.
Modify a MongoDB Index
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. Download our MongoDB GUI and be on your way to faster query performance.
Speaking of queries, have you checked out our Visual Query Builder yet? It’s the easiest, quickest way to build a MongoDB query – just drag, drop, and done. Or if you’re into writing out your queries, save time with IntelliShell and autocomplete them down to field names.
Editor’s Note: This post was originally published in November 2014.