Don't Forget to do this if you're using MongoDB
Tags: mongodb, node, database
Published: 2022-05-12 16:58:05


## 🗂 Whats inside this blog ?

1.  What is Indexing
2.  When to do Indexing
3.  Seeing differences between indexed and unindexed queries
4.  COLLSCAN and IXSCAN
5.  How to do indexing

---

## 🙇🏽‍♂️ What is indexing ?

- Indexing is a way to **improve fetch speed** by **arranging the  document in some way** so that **retrieval is going to be faster**.

- **id** of every document is unique and it is stored in a certain order ( ascending by default ) so any documents that is fetched using id will be **faster** when compared to other fields. This is because **id is automatically index** by mongodb 

- MongoDB **indexing uses B-Tree , B+Tree** to sort the indexes which is similar to **Binary Search Tree.**. [More Info on MongoDB Indexes](https://www.mongodb.com/docs/manual/indexes/)

---
## 👍🏽 When should we use indexes ?

- Whenever the **document insertion operations are less than fetch operatation**, indexes are pretty good. 

- That is because, Mongodb internally **organizes the indexed documents in certain order** that follows B-Tree ( What is B-Tree , thats for another day). So for each insertion the structure of tree changes , MongoDB has to arrange the tree with respect to the inserted document  and its really time consuming when the number of documents grow exponentially.

- It is recommended to use it whenever you're q**uerying without ids**.

> 🥳  Fun Fact -> Firestore won't allow querying without indexing

---

## 🌞 🌚 Seeing Differences between unindexed and indexed query

I have a db called **FORUM** and collection called **accountDetails** that has 3 documents having **userId** as one of the fields.

Let me just fetch one of the documents.

![No Index](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8o633p2rkts8mxls34r5.png)

1. Blue - Querying using mongoDb compass for one document using **userId**
2. Brown - mongoDb searches for 3 documents and finds a match then returns 1 matched document
3. Red - Warning for no index
4. Violet - COLLSCAN is called.


## 🪓 Now let us create indexes for **userId**
![Creating Index](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/skjprb4lj4ahpzmyqcqg.png)

You can create these from index tab either from mongoDB compass or from web version also. Both will have the same user interface.

Just select the fields that is to feteched and there are several layers to it such as compound indexing and other stuff. [More Info on MongoDB Indexes](https://www.mongodb.com/docs/manual/indexes/)

## 🤯 Query the same userId now

![Indexed Query](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3e79vak2wlckj1h2fre.png)

1. You can see now we're using index to fetch.
2. Only one document is fetched and that document is returned.
3. Using IXSCAN instead of COLLSCAN  


## 🤓 COLLSCAN and IXSCAN
- **COLLSCAN** uses the entire collection to fetch a query.

- An average business that has 1 Lak documents on a collection. To query a un indexed document, mongoDB has to go through 1 lak docs to get one doc.

- Which will be really be heavy on processing and will take **several hundred milliseconds** to execute a query.

On the other hand
- **IXSCAN** uses the arranged b-tree structure and just returns the exact document needed. No need to go through the entire collection 

- Because thats why we use index  , duh 🤷🏽! To know where stuff is.


_There is a parameter in the above images Actual Query Execution time (ms) : 0. In both cases it is 0 but if the number of documents is higher ( let’s say 1,00,000 ) Then also **IXSCAN is going to be near 0ms** but not the COLLSCAN (several hundred milliseconds)._


---

🔖 Don't forget to bookmark this for future references. 
Give me follow on devto if you like this blog. 

If you are interested in web design check out [Top CSS sites](https://dev.to/shrihari/colors-design-inspiration-websites-for-new-comers-1lcb)
Are you working as web developer ? Check out [PWA sites I use on my laptop](https://dev.to/shrihari/top-pwa-apps-i-use-everyday-laptop--4pbn)
You planning to create a new portfolio site ? [Shrihari Mohan](https://shrihari-portfolio.vercel.app/) template at the bottom of website.

Follow me on [Twitter](https://twitter.com/papashrihari) 

Thanks & Cheers🍻 !

---
## Try Our new product for free!
[DocsAI](https://docsai.app/) - Create AI support agents with your documents in the most affordable price, starts at **0$**. Don't need a bot , but need ai help on your docs just upload and start chating !

Using for a company ? [Check out our pricing](https://docsai.app/pricing) Just contact me for personalized pricing !

![docsAi](https://firebasestorage.googleapis.com/v0/b/shrihari-personal-storage.appspot.com/o/banner-docs-jpeg.jpeg?alt=media&token=45c47666-de2a-4e02-8881-beb6e43ce581)

---