<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Irfat</title>
    <description>The latest articles on DEV Community by Irfat (@irfat7).</description>
    <link>https://dev.to/irfat7</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F563667%2F21ca323a-34bc-4766-ba5a-96885617603c.jpeg</url>
      <title>DEV Community: Irfat</title>
      <link>https://dev.to/irfat7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/irfat7"/>
    <language>en</language>
    <item>
      <title>Using Indexing To Optimize MongoDB Performance</title>
      <dc:creator>Irfat</dc:creator>
      <pubDate>Thu, 30 May 2024 17:16:03 +0000</pubDate>
      <link>https://dev.to/irfat7/using-indexing-to-optimize-mongodb-performance-4b40</link>
      <guid>https://dev.to/irfat7/using-indexing-to-optimize-mongodb-performance-4b40</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Every system must have its database performance &lt;strong&gt;optimized&lt;/strong&gt; in order to function properly. It improves the app's overall performance. When data grows, an efficient database system can handle the extra load without experiencing noticeable slowdowns and process data much more quickly and improve user experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Indexing?
&lt;/h3&gt;

&lt;p&gt;Database indexing is a technique for improving it's performance by speeding up &lt;strong&gt;read&lt;/strong&gt; operations. It is similar to the indexes used in books. It efficiently guides database queries, resulting in overall improved performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Indexing?
&lt;/h3&gt;

&lt;p&gt;Indexing can be used to improve overall performance of MongoDB. Indexes &lt;strong&gt;decrease&lt;/strong&gt; the amount of data that query operations must process, which &lt;strong&gt;increases&lt;/strong&gt; the performance of read operations. This lessens the effort involved in responding to requests in MongoDB. For this tutorial, we'll look at a simple MongoDB collection called &lt;code&gt;students&lt;/code&gt;, which has the following field in each document.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//for this example we will consider a simple number&lt;/span&gt;
    &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//name of students&lt;/span&gt;
    &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//age of students&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Assume that the &lt;code&gt;students&lt;/code&gt; collection contains the following documents.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;_id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;age&lt;/th&gt;
&lt;th&gt;gender&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;25&lt;/td&gt;
&lt;td&gt;female&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;male&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;td&gt;male&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;David&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;male&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Eve&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;td&gt;female&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Query Performance Without Indexing
&lt;/h3&gt;

&lt;p&gt;Now, without indexing, let us find 'Eve' using the &lt;code&gt;db.students.find({name: 'Eve', age: 22}).explain('executionStats')&lt;/code&gt; method. Here &lt;code&gt;explain('executionStats')&lt;/code&gt; method provides information about the performance of the query and it returns following object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;queryPlanner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="p"&gt;...&lt;/span&gt;
         &lt;span class="na"&gt;winningPlan&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;stage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;COLLSCAN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;...&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="nx"&gt;executionStats&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;executionSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;nReturned&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;executionTimeMillis&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;totalKeysExamined&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;totalDocsExamined&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;executionStages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nl"&gt;stage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;COLLSCAN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="p"&gt;...&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;queryPlanner.winningPlan.stage: 'COLLSCAN'&lt;/code&gt; means this query did not use any indexing rather it performed a &lt;strong&gt;collection scan&lt;/strong&gt; which is basically searching one document after another which is generally an expensive process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;executionStats.nReturned: 1&lt;/code&gt; indicates that this query returned only 1 document.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;executionStats.totalKeysExamined: 0&lt;/code&gt; shows that this query is not using any indexing for search.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;executionStats.totalDocsExamined: 5&lt;/code&gt; indicates this query scanned total of &lt;strong&gt;5 documents&lt;/strong&gt; i.e. entire collection is scanned.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Query Performance With Indexing
&lt;/h3&gt;

&lt;p&gt;Before creating an index let's find the current available indexes. To get the current indexes we can use &lt;code&gt;db.students.getIndexes()&lt;/code&gt; method, it will return the following array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;_id_&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;key&lt;/code&gt; means the &lt;code&gt;field name&lt;/code&gt; based on which the indexing is created that means the &lt;em&gt;&lt;code&gt;_id&lt;/code&gt; field is already indexed by default&lt;/em&gt;. So, searching with &lt;code&gt;_id&lt;/code&gt; already performed using an index.&lt;/p&gt;

&lt;p&gt;Let's create index for &lt;code&gt;age&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt; field of our collection where &lt;code&gt;age&lt;/code&gt; will be in &lt;strong&gt;ascending order&lt;/strong&gt; and &lt;code&gt;name&lt;/code&gt; will be in &lt;strong&gt;descending order&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To create the index we can use &lt;code&gt;db.students.createIndex({age:1, name: -1})&lt;/code&gt; &lt;code&gt;1&lt;/code&gt; means ascending and &lt;code&gt;-1&lt;/code&gt; means descending order. Calling the &lt;code&gt;getIndexes()&lt;/code&gt; method again it will return the following array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;_id_&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;age_1_name_-1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;New index with name &lt;code&gt;age_1_name_-1&lt;/code&gt; is created.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Creating an index on the same field will throw an error. You need to &lt;em&gt;delete&lt;/em&gt; the previous index using the &lt;a href="https://www.mongodb.com/docs/manual/reference/method/db.collection.dropIndex/"&gt;&lt;strong&gt;db.collection.dropIndex()&lt;/strong&gt;&lt;/a&gt; method first.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, let's run the query &lt;code&gt;db.students.find({name: 'Eve', age: 22}).explain('executionStats')&lt;/code&gt; again and it returns the following result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;queryPlanner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="na"&gt;winningPlan&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;stage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;FETCH&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;inputStage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;stage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;IXSCAN&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;keyPattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;indexName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;age_1_name_-1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="nx"&gt;executionStats&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nl"&gt;executionSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="nx"&gt;nReturned&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="nx"&gt;executionTimeMillis&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="nx"&gt;totalKeysExamined&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="nx"&gt;totalDocsExamined&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="nx"&gt;executionStages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="p"&gt;...&lt;/span&gt;
         &lt;span class="p"&gt;},&lt;/span&gt;
         &lt;span class="p"&gt;...&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;queryPlanner.winningPlan.inputStage.stage: 'IXSCAN'&lt;/code&gt; indicates that this time it used &lt;strong&gt;index scanning&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;executionStats.nReturned: 1&lt;/code&gt; means this query returned 1 document.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;executionStats.totalKeysExamined: 1&lt;/code&gt; shows that this query is used &lt;strong&gt;one key&lt;/strong&gt; for the execution of the query.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;executionStats.totalDocsExamined: 1&lt;/code&gt; means this query scanned &lt;strong&gt;only one document&lt;/strong&gt; which previously was 5.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, using index scanning made a huge improvement.&lt;/p&gt;

&lt;h3&gt;
  
  
  How MongoDB IXScan Works
&lt;/h3&gt;

&lt;p&gt;Similar to other databases, MongoDB employs &lt;a href="https://www.programiz.com/dsa/b-tree"&gt;&lt;strong&gt;B-trees&lt;/strong&gt;&lt;/a&gt; to store indexes. If a document has n number of collections then using &lt;code&gt;COLLSCAN&lt;/code&gt; has complexity of &lt;code&gt;O(n)&lt;/code&gt; since it needs to check all the documents for worst case on the other hand B-tree has &lt;strong&gt;better&lt;/strong&gt; searching complexity which is &lt;code&gt;O(log n)&lt;/code&gt;. Indexing scans the B-tree for the key and then returns the documents to which the key points. MongoDB indexing can be described in following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initiating an Empty B-tree:&lt;/strong&gt; When you use the &lt;code&gt;createIndex()&lt;/code&gt; method in MongoDB, an &lt;strong&gt;empty B-tree&lt;/strong&gt; is created for the index. The B-tree is initially empty and will be filled out as documents are added to the collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Updating the Tree Upon Insertion:&lt;/strong&gt; As documents are inserted into the collection, the B-tree index is updated to reflect these insertions. The B-tree is maintained in a balanced state to ensure efficient querying.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Traversing the Tree for Queries:&lt;/strong&gt; When you execute a query that can utilize an index, MongoDB traverses the B-tree to find the &lt;strong&gt;matching&lt;/strong&gt; documents efficiently. This traversal involves navigating the B-tree based on the values being queried. If the value is less than or equal to a node it will traverse to &lt;strong&gt;left&lt;/strong&gt; subtree else &lt;strong&gt;right&lt;/strong&gt; subtree.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Leaf Nodes Pointing to Documents:&lt;/strong&gt; In a B-tree index, the leaf nodes typically contain &lt;strong&gt;references&lt;/strong&gt; (pointers) to the actual documents in the collection that match the indexed values. This allows MongoDB to quickly locate the documents that satisfy the query conditions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Selecting Documents from Pointers:&lt;/strong&gt; When MongoDB finds leaf nodes containing pointers to documents, it retrieves those documents from the collection. These documents are then returned as query results.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The image below can help to summarize the entire process.&lt;/p&gt;

&lt;p&gt;
  &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdhwgtvbgmts3v429ivsa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdhwgtvbgmts3v429ivsa.png" alt="Image description" width="800" height="418"&gt;&lt;/a&gt;
  &lt;/p&gt;
&lt;center&gt;&lt;em&gt;Fig: IXSCAN scan visualization&lt;/em&gt;&lt;/center&gt;
  &lt;br&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In MongoDB, the internal structure of non-leaf nodes within B-tree indexes is determined by the MongoDB server &lt;strong&gt;itself&lt;/strong&gt;. These non-leaf nodes serve as guides for queries, directing them towards the appropriate leaf nodes where the indexed data is stored.&lt;/p&gt;


&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Execution Of The Query Using COLLSCAN
&lt;/h4&gt;

&lt;p&gt;
  &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7jst91sa9lqpjwjqp448.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7jst91sa9lqpjwjqp448.png" alt="Image description" width="800" height="418"&gt;&lt;/a&gt;
  &lt;/p&gt;
&lt;center&gt;&lt;em&gt;Fig: COLLSCAN execution&lt;/em&gt;&lt;/center&gt;
  &lt;br&gt;

&lt;h4&gt;
  
  
  Execution Of The Query Using IXSCAN
&lt;/h4&gt;

&lt;p&gt;
  &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2a1bns59d3933r4s299.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2a1bns59d3933r4s299.png" alt="Image description" width="800" height="329"&gt;&lt;/a&gt;
  &lt;/p&gt;
&lt;center&gt;&lt;em&gt;Fig: IXSCAN execution&lt;/em&gt;&lt;/center&gt;
  &lt;br&gt;

&lt;h3&gt;
  
  
  Avoid Indexing When
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A collection has a &lt;strong&gt;high&lt;/strong&gt; write-to-read ratio, and indexing are costly because each insert requires updating any indexes.&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;smaller&lt;/strong&gt; databases, implementing indexing may not provide a substantial improvement in performance. The benefits of indexing become more apparent as the size of the database grows, but for smaller datasets, the performance gains might be minimal.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Indexing is an excellent approach to improve the performance of your database. It enhances the user experience by making a system quicker. This can be accomplished by indexing the most frequently used fields of a collection. It is also crucial to note that indexing has its drawbacks. It demands more storage and processing. So, it's important to understand when to utilize it and when not to.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>database</category>
      <category>development</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
