Adding and Modifying MongoDB Indexes with MongoChef

MongoChef makes it very easy to add and even modify existing MongoDB indexes. Let’s start by taking a look at adding a new index.

Adding a New MongoDB Index

In MongoChef, connect to your MongoDB server. In the left-hand pane, you’ll see the databases and their collections available on the server. 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 in the next figure.

MongoDB GUI: Add Index

This will open the “Add Index” view for you. 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.

MongoDB GUI: Visual MongoDB Index Editor

Schema Scan to Present Available MongoDB Index Fields

MongoChef 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 did not cover your desired field, enter the field name directly, and click “Add”.

MongoDB GUI: MongoDB Index Editor with Field Selection

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.

MongoDB GUI: MongoDB Index Setup

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.

MongoDB GUI: Create MongoDB Index in Background

Text Index Options

In the “Text Options”, you can set the index options that are specific to text indexes.

MongoDB Index Text Options

For example, click the “Default Language” combo box to choose your desired default language of your text index fields.

MongoDB GUI: Index Text Options and Language Settings

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.

MongoDB GUI: Index Text Options and Weight

Geo Index Options

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

MongoDB GUI: Index Geo Options

Overwriting JSON Code

MongoChef 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!

MongoDB GUI: Manual JSON Index Setup

Once you have configured your index, click “Create Index” and the index will be built on thee server. You may close the index view tab at any point, the index build will continue on the server.

Modifying an Existing 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, MongoChef 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.

Modify Existing MongoDB Index

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

Modify Existing MongoDB Index with MongoDB GUI

Of course, you can also manually overwrite the JSON code again.

Modify Existing MongoDB Index with JSON Editor

To re-create the index with the modifications you have made, simply click “Re-create Index” and MongoChef will take care of the rest. It’s as easy as that!