The concepts of building indexes in MongoDB are very similar to those of relational databases like MySQL, SQL Server etc. You can read about these concepts and theory on this webpage. In this tutorial, we’ll cover how MongoVUE GUI makes it easy to create MongoDB indexes.

We’ll refer to the following example used in the 10Gen documentation:

db.factories.insert( { name: "xyz", metro: { city: "New York", state: "NY" } } );

// alternative A
db.factories.ensureIndex( { metro : 1 } );

// alternative B
db.factories.ensureIndex( { "metro.city" : 1, "metro.state" : 1 } );

 

Step 1:

Let’s fire up MongoVUE and navigate to our “infrastructure” database to list “factories” collection

image

 

Step 2:

Right-click on “factories” collection, and select “Add Index…” from the context-menu. This will launch the new index window.

image

 

Now there are 2 options to build indexes. You can either type the Json under the “Json” tab or you can build it visually as shown in the steps below.

Step 3:

Click on the “Visual” tab.

image

 

Step 4:

Now select the fields on which you want to build the index. In our example, we’ll select “metro”, and select an “Ascending” index on it.

image

That’s it. Hit “Create” button and you are done.

You can also choose to expand “metro” key into its sub-keys, and use those to build the index (see below):

image

Again, simply hit the “Create” button and you are done!

Things to note:

  • MongoVUE does not scan each and every document in your collection to show an aggregated document under “Visual” tab. It scans the first 5 documents only.
  • If these first 5 documents do not contain all the fields then you may index some fields under “Visual” tab and then manually switch to “Json” tab to add the additional ones before hitting “Create” button

Tags: , , ,