Features Of MongoDB

Lokesh
3 min readJun 16, 2020

Indexing

It is used to speed up the look up operation but it causes the insertion to be slow. Let’s take an example if you reading a book and you want to see some topics. So what we generally do is we check the index of book for page number and directly go to that page. Similarly in MongoDB we create index and when we any specific record then instead of going through entire collection we just take it with the help of index.

Some common types

Single Field — It creates a lookup table for that specific field and reduces the time to search any record.

Compound — A single index uses multiple fields to create the look up table. We choose the order in which the index should be stored.

Unique — Mongo also allows the field to be unique, this has to be created before inserting the document. Like you can have user name field set to unique so that there will be no duplicates in the usernames.

Let’s understand the indexing in mongo with some example,

By default mongo db creates an unique Index on _id field. The index is created and stored in local disk, but the application is running again and again it is loaded in the RAM for faster access. db.collection.getIndexes() is used to view all the indexes created for that collection.

Index

As we can see that we have a default index on _Id field created, and lets check how many records are scanned when i look for any field in the collection.

So you can see under execution stats, number of docs examined are 29, now lets add indexing to one of its field.

Now the index is created on tourTags field, Now let’s execute the same search query and see how much records it scanned to find the document.

Now we can see that the total docs examined are 3. Since the size of db is very small so it does not make any noticeable effect but when the size of our db is very large then it is really helpful in improving the execution time.

Sharding

Sharding is a powerful feature in mongo. It allows the database to be scaled horizontally. We can have a mongo database stored in multiple systems say one part of data in one system and the other part in the other system. So we do not need to scale the system vertically. But yeah when this is done the performance is bit slow.

Reliability

What will happen when the primary server goes down? Our app will crash, So for this mongo maintains the replicated copies of our data in secondary servers, when any of then fails then it assign any secondary server as primary. When the server which was down is ready and up then it assigns as secondary server. So MongoDB is also reliable in terms of horizontal scaling.

--

--