Try Studio 3T for Free
  1. Knowledge Base
  2. Performance
  3. How to Create a MongoDB Index

How to Create a MongoDB Index

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.

Don’t have Studio 3T on your machine yet? Download the latest version, available for Windows, Mac, and Linux.

Add a 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.

Right-click on a collection and choose Add Index

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.

Add a name and choose the fields for your MongoDB index

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”.

Choose the fields you want added to your MongoDB index

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.

In Studio 3T, you can choose the index type by clicking on the drop-down menu arrow

MongoDB index options

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.

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.

Under Text Options, you can easily set the default language of your newly-created MongoDB index

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.

Geo index options

Analogously, in the “Geo Options” tab, you can set index options specific to the different Geo index types.

You can also set the geo options on our MongoDB GUI

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.

You can also set partial index options in Studio 3T, and even validate JSON before saving

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!

Under the JSON tab, Studio 3T also lets you overwrite the JSON code

Use custom collation

Under the last tab “Collation“, you can use custom collation by choosing values for the predefined settings.

You can choose values for the predefined settings and use custom collation for your MongoDB index

What’s collation again? Read more about it here

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.

The MongoDB Studio 3T makes it very easy to modify a MongoDB index - simply right click, and done!

Studio 3T will then retrieve all information about that index and open the familiar index view with all parameters filled already filled in.

You can easily edit your MongoDB index and change the existing parameters

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.

Once you have made your modifications, click on Re-create Index to update your MongoDB index

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 Studio 3T and be on your way to faster query performance.

Want to take indexing to another level?

Updated on February 24, 2021

Was this article helpful?

Related Articles

Leave a Comment