Before going in detail about types of indexes in MongoDB let’s talk a little about what indexes does in your database and how it help you to speed up your database search queries.
When we create index on table field what actually does is create meta data on that field of a table and point it to original field along with that it sort the meta data that is just created for that particular field this way when we run query on that specific field, in background database will search on that meta data that is just created as it’s already sorted the database will find the data faster than we search on the main table.
Now that you know a little about index what it does and if you don’t please research a little about how index works more as in here I’m going to write specifically about types of index in MonogDB if you know the logic of how index works then you will be fine.
Regular or B-Tree index
This kind of indexes is very common in Relation Database as well and in Mongo also you can have this kinds of index in any document that suites your needs, in this types of index you can index single field, multiple field of even sets of array can be used to add index in MongoDB.
Geo index
This types of index is as the name suggests is useable mostly to store geographical information and it works well for those kinds of data but it does not have to be geographical information only you can store other kinds of data and index them as well by using geo index, you can use this mostly in finding the location in proximity like (find nearest location to some places), this is mostly optimized to query location indexes.
Text index
These kinds of indexes in mongo is used to index text base documents like if you have document that you store text in them like E-mails etc. then you can use this index, the big example would be search engines that they use this kinds of index to optimize search queries as you know search engines crawl the web and index the content of the web and having this kind of index will help to speed up the search queries.
Hash index
This kinds of indexes in mongo supports sharding, what you do is index some certain fields and distribute them evenly between clusters of servers, this will allow you to sort your storage in the cluster environment evenly.
TTL index
And finally there is Time to Live index, what this will do you put index in some certain fields and let them have a Expiry date what mongo does is when the expiry date reaches it will delete the document automatically from your collection.
This will save your time and overhead to write manual scripts in order to delete those documents from your database instead let MongoDB to do the work for you by using this kinds of indexes in the field of data you have.
1 Comment to Types of Indexes in MongoDB