<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Indexes in MongoDB &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/indexes-in-mongodb/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Wed, 07 Mar 2018 16:02:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Indexes in MongoDB &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Continue With Learning Indexes in MongoDB</title>
		<link>https://www.sibeeshpassion.com/continue-with-learning-indexes-in-mongodb/</link>
					<comments>https://www.sibeeshpassion.com/continue-with-learning-indexes-in-mongodb/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 07 Mar 2018 16:02:02 +0000</pubDate>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Indexes in MongoDB]]></category>
		<category><![CDATA[Mongo Commands]]></category>
		<category><![CDATA[Mongo Shells]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=12673</guid>

					<description><![CDATA[[toc] Introduction This is the third article of the series &#8220;Learn MongoDB with me&#8221;, if you haven&#8217;t read my previous post on this topic, I strongly recommend you to find it here. This is the continuation of exploring the Indexes on MongoDB, we will be discussing about various MongoDB indexes which we can perform on our data. I hope you will find this post useful. Thanks for reading. Learn MongoDB with me You can see all the articles on this series below. Learn MongoDB with me Learn MongoDB with me &#8211; Part 2 Using MongoDB on Node JS Application Using [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h2>Introduction</h2>
<p>This is the third article of the series &#8220;Learn MongoDB with me&#8221;, if you haven&#8217;t read my previous post on this topic, I strongly recommend you to find it <a href="http://sibeeshpassion.com/category/mongodb/">here</a>. This is the continuation of exploring the Indexes on MongoDB, we will be discussing about various MongoDB indexes which we can perform on our data. I hope you will find this post useful. Thanks for reading.</p>
<h2>Learn MongoDB with me</h2>
<p>You can see all the articles on this series below.</p>
<ul>
<li><a href="http://sibeeshpassion.com/learn-mongodb-with-me/">Learn MongoDB with me</a></li>
<li><a href="http://sibeeshpassion.com/learn-mongodb-with-me-part-2/">Learn MongoDB with me &#8211; Part 2</a></li>
<li><a href="http://sibeeshpassion.com/using-mongodb-on-node-js-application-using-mongoose/">Using MongoDB on Node JS Application Using Mongoose</a></li>
</ul>
<h2>Background</h2>
<p>Like I said, it is going to be the third  part of the series. I believe that you have enough knowledge about Mongo DB now,  If not, please consider reading my <a href="http://sibeeshpassion.com/category/mongodb/">previous posts</a> again.</p>
<h2>Indexes in MongoDB</h2>
<p>Let&#8217;s import a new collection, <em>p</em><em>roducts</em> first.<em> </em></p>
<pre class="EnlighterJSRAW" data-enlighter-language="json">[
   {
      "id":2,
      "name":"An ice sculpture",
      "price":12.50,
      "tags":[
         "cold",
         "ice"
      ],
      "dimensions":{
         "length":7.0,
         "width":12.0,
         "height":9.5
      },
      "warehouseLocation":{
         "latitude":-78.75,
         "longitude":20.4
      }
   },
   {
      "id":3,
      "name":"A blue mouse",
      "price":25.50,
      "dimensions":{
         "length":3.1,
         "width":1.0,
         "height":1.0
      },
      "warehouseLocation":{
         "latitude":54.4,
         "longitude":-32.7
      }
   },
   {
      "id":4,
      "name":"Keyboard",
      "price":15.50,
      "dimensions":{
         "length":1.1,
         "width":1.0,
         "height":1.0
      },
      "warehouseLocation":{
         "latitude":24.4,
         "longitude":-42.7
      }
   },
   {
      "id":5,
      "name":"Doll",
      "price":10.50,
      "dimensions":{
         "length":5.1,
         "width":1.0,
         "height":7.0
      },
      "warehouseLocation":{
         "latitude":64.4,
         "longitude":-82.7
      }
   },
   {
      "id":6,
      "name":"Wallet",
      "price":5.50,
      "dimensions":{
         "length":1.1,
         "width":1.0,
         "height":1.0
      },
      "warehouseLocation":{
         "latitude":24.4,
         "longitude":-12.7
      }
   }
]</pre>
<p>Please be noted that these are just dummy data, and it may sound illogical to you.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">C:\Program Files\MongoDB\Server\3.4\bin&gt;mongoimport --db mylearning --collection products --jsonArray --file products.json
2018-03-06T16:48:34.440+0530    connected to: localhost
2018-03-06T16:48:34.607+0530    imported 5 documents

C:\Program Files\MongoDB\Server\3.4\bin&gt;</pre>
<p>If you don&#8217;t know how the import command works, please read my previous posts where we have seen simple indexes.  Now we have the data, let&#8217;s go perform Indexes.</p>
<h3>Single Key Indexes</h3>
<p>In one of my previous post in this series of article, I had mentioned about simple indexes. Here in this article, we are not going to talk about it, instead  we will explore on other indexing option what MongoDB has. Sounds good? If yes, let&#8217;s continue. let&#8217;s go and see Multi key indexes</p>
<h3>Multi Key Indexes or Compound Indexes</h3>
<p>As the name implies, we are actually going to set indexes with more than one key element. On our products collection, we have some product documents right, what we a user needs to filter the same with the price and warehouse location. Yeah, we need to build a query.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; db.products.find({
... "price: {$lte: 16},
2018-03-06T17:10:15.005+0530 E QUERY    [thread1] SyntaxError: unterminated string literal @(shell):2:0
MongoDB Enterprise &gt; db.products.find({
... "price": {$lte: 16},
... "warehouseLocation.latitude": {$gte: 60}
... })
{ "_id" : ObjectId("5a9e790a1ae1f955c1a70c4a"), "id" : 5, "name" : "Doll", "price" : 10.5, "dimensions" : { "length" : 5.1, "width" : 1, "height" : 7 }, "warehouseLocation" : { "latitude" : 64.4, "longitude" : -82.7 } }
MongoDB Enterprise &gt;</pre>
<p>We have got one entry according to  our search, <em>&#8220;price&#8221;: {$lte: 16}</em> and <em>&#8220;warehouseLocation.latitude&#8221;: {$gte: 60} </em>that&#8217;s cool. Now let&#8217;s try to find out the execution status for the same.</p>
<p>Please be noted that we have used <em>$lte</em> and <em>$gte</em> which stands for &#8220;less than or equal to&#8221; and &#8220;greater than or equal to&#8221;, remember what I have told you before, &#8220;Mongo shell is cool and we can do anything with it&#8221;. Let&#8217;s find out the examined elements count for our preceding find query now.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">db.products.find({ "price": {$lte: 16}, "warehouseLocation.latitude": {$gte: 60} }).explain("executionStats")</pre>
<p>And if your query if correct, you will be getting a result as preceding.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="json">"queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "mylearning.products",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [
                                {
                                        "price" : {
                                                "$lte" : 16
                                        }
                                },
                                {
                                        "warehouseLocation.latitude" : {
                                                "$gte" : 60
                                        }
                                }
                        ]
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "$and" : [
                                        {
                                                "price" : {
                                                        "$lte" : 16
                                                }
                                        },
                                        {
                                                "warehouseLocation.latitude" : {
                                                        "$gte" : 60
                                                }
                                        }
                                ]
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 107,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 5,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "$and" : [
                                        {
                                                "price" : {
                                                        "$lte" : 16
                                                }
                                        },
                                        {
                                                "warehouseLocation.latitude" : {
                                                        "$gte" : 60
                                                }
                                        }
                                ]
                        },
                        "nReturned" : 1,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 7,
                        "advanced" : 1,
                        "needTime" : 5,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "direction" : "forward",
                        "docsExamined" : 5
                }
        },
        "serverInfo" : {
                "host" : "PC292716",
                "port" : 27017,
                "version" : "3.4.9",
                "gitVersion" : "876ebee8c7dd0e2d992f36a848ff4dc50ee6603e"
        },
        "ok" : 1
}</pre>
<p>You might have already noticed the value we have for <em>totalDocsExamined</em> , if you haven&#8217;t please check now. In my case it is 5, which means the query just examined all the records we have. Ah, that sounds bad right? What if we have millions of records on our collection, how long it is gonna take to fetch the results?</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; db.products.createIndex({price:1, "warehouseLocation.latitude":1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}</pre>
<p>Run your previous query now, and find out what is the value of docs examined.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; db.products.find({ "price": {$lte: 16}, "warehouseLocation.latitude": {$gte: 60} }).explain("executionStats")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "mylearning.products",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [
                                {
                                        "price" : {
                                                "$lte" : 16
                                        }
                                },
                                {
                                        "warehouseLocation.latitude" : {
                                                "$gte" : 60
                                        }
                                }
                        ]
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "price" : 1,
                                        "warehouseLocation.latitude" : 1
                                },
                                "indexName" : "price_1_warehouseLocation.latitude_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "price" : [ ],
                                        "warehouseLocation.latitude" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "price" : [
                                                "[-inf.0, 16.0]"
                                        ],
                                        "warehouseLocation.latitude" : [
                                                "[60.0, inf.0]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 1089,
                "totalKeysExamined" : 5,
                "totalDocsExamined" : 1,
                "executionStages" : {
                        "stage" : "FETCH",
                        "nReturned" : 1,
                        "executionTimeMillisEstimate" : 310,
                        "works" : 5,
                        "advanced" : 1,
                        "needTime" : 3,
                        "needYield" : 0,
                        "saveState" : 2,
                        "restoreState" : 2,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 1,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 1,
                                "executionTimeMillisEstimate" : 270,
                                "works" : 5,
                                "advanced" : 1,
                                "needTime" : 3,
                                "needYield" : 0,
                                "saveState" : 2,
                                "restoreState" : 2,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                        "price" : 1,
                                        "warehouseLocation.latitude" : 1
                                },
                                "indexName" : "price_1_warehouseLocation.latitude_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "price" : [ ],
                                        "warehouseLocation.latitude" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "price" : [
                                                "[-inf.0, 16.0]"
                                        ],
                                        "warehouseLocation.latitude" : [
                                                "[60.0, inf.0]"
                                        ]
                                },
                                "keysExamined" : 5,
                                "seeks" : 4,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : "PC292716",
                "port" : 27017,
                "version" : "3.4.9",
                "gitVersion" : "876ebee8c7dd0e2d992f36a848ff4dc50ee6603e"
        },
        "ok" : 1
}
MongoDB Enterprise &gt; db.products.find({ "price": {$lte: 16}, "warehouseLocation.latitude": {$gte: 60} })
{ "_id" : ObjectId("5a9e790a1ae1f955c1a70c4a"), "id" : 5, "name" : "Doll", "price" : 10.5, "dimensions" : { "length" : 5.1, "width" : 1, "height" : 7 }, "warehouseLocation" : { "latitude" : 64.4, "longitude" : -82.7 } }
MongoDB Enterprise &gt;</pre>
<p>Yeah, we got <em>&#8220;docsExamined&#8221; : 1 </em>, that&#8217;s the way to go. Go create some indexes on your top most queries, you can definitely see some magics over there. You can create up to 64 indexes on a collection in MongoDB, but you may need to create only few, only on your top result queries. What you can do is, whenever you are facing any performance issues on any queries, consider that it needs some tuning and definitely a Index. There are so many other complex Indexes, but widely used Indexes are single key index and compound index.</p>
<p>With that, we are done with this post. I will be posting the continuation part of this series very soon. Till then, bye.</p>
<h2><span id="conclusion">Conclusion</span></h2>
<p>Thanks a lot for reading. Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h2><span id="your-turn-what-do-you-think">Your turn. What do you think?</span></h2>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/continue-with-learning-indexes-in-mongodb/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Learn MongoDB With Me</title>
		<link>https://www.sibeeshpassion.com/learn-mongodb-with-me/</link>
					<comments>https://www.sibeeshpassion.com/learn-mongodb-with-me/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Thu, 22 Feb 2018 05:03:00 +0000</pubDate>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Indexes in MongoDB]]></category>
		<category><![CDATA[MongoDB Basics]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[Why MongoDB]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=12641</guid>

					<description><![CDATA[[toc] Introduction This is going to be a series of article on MongoDB. Here we are going to do some exercises with MongoDB, we will be talking about Mongo Shell, how can we configure MongoDB?, What are Indexes in MongoDB etc. We all know what an Indexes is, you might have already done that with any relational databases like SQL and MySQL . Have you every done indexing for your MongoDB? If your answer is &#8220;no&#8221;, no worries, here we are going to see indexes in MongoDB , if it is a &#8220;yes&#8221; please read this post and correct me if I am [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h2>Introduction</h2>
<p>This is going to be a series of article on MongoDB. Here we are going to do some exercises with MongoDB, we will be talking about Mongo Shell, how can we configure MongoDB?, What are Indexes in MongoDB etc. We all know what an Indexes is, you might have already done that with any relational databases like <a href="http://sibeeshpassion.com/category/SQL/">SQL</a> and <a href="http://sibeeshpassion.com/category/MySQL/">MySQL</a> . Have you every done indexing for your MongoDB? If your answer is &#8220;no&#8221;, no worries, here we are going to see indexes in <a href="http://sibeeshpassion.com/category/mongodb/">MongoDB</a> , if it is a &#8220;yes&#8221; please read this post and correct me if I am wrong anywhere. Let&#8217;s begin now.</p>
<h2>Prerequisites</h2>
<p>I hope you might have got a basic information about MongoDB, if not, I strongly recommend you to read <a href="http://sibeeshpassion.com/category/mongodb/">these posts</a>. Now that, you have a basic idea, I am assuming that you have already set up the environment for MongoDB development. Let&#8217;s recall what you might have done so far.</p>
<ol>
<li>Install MongoDB</li>
<li>Set the environment variable for MongoDB</li>
<li>Start the MongoDB services</li>
</ol>
<p>To set the environment variable for MongoDB, you may have to add a new path to the system variable path with the value as &#8220;C:\Program Files\MongoDB\Server\3.4\bin&#8221;, please note that the version number will be varied according to your MongoDB version. Once you are done the above steps, you should be able to start both Mongo server and Mongo shell from the command line interface.</p>
<h2>Setting up MongoDB using CLI</h2>
<p>Now let&#8217;s just open our command line interface, and create the data directory for Mongo. We will have to create a directory for the same. Please go along with the below commands.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">md \data
md \data\db
mongod</pre>
<p>Now let&#8217;s open a new CLI and run the command &#8220;mongo&#8221;, please do not worry about the warnings you are getting, as we are not working in production data, we may not need to secure and optimize it.</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2018/02/Mongo-Command.png"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-12643" src="http://sibeeshpassion.com/wp-content/uploads/2018/02/Mongo-Command.png" alt="" width="472" height="176" srcset="/wp-content/uploads/2018/02/Mongo-Command.png 472w, /wp-content/uploads/2018/02/Mongo-Command-300x112.png 300w, /wp-content/uploads/2018/02/Mongo-Command-400x149.png 400w" sizes="(max-width: 472px) 100vw, 472px" /></a></p>
<h2>Exploring MongoDB</h2>
<p>Once you are connected to MongoDB, by default you are connected to test DB. You can check that by running the command <code class="EnlighterJSRAW" data-enlighter-language="null">MongoDB Enterprise &gt; db</code></p>
<h3>Playing with Mongo Shell</h3>
<p>Let&#8217;s just use a new database now.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; use MongoIndex
switched to db MongoIndex
MongoDB Enterprise &gt;</pre>
<p>Please be noted that the database MongoIndex doesn&#8217;t exist as of now, as we haven&#8217;t created it. Still, Mongo just switched our context to the new database. You can see this if you run the command <code class="EnlighterJSRAW" data-enlighter-language="shell">show dbs</code></p>
<p>The database will be created once we insert any document associated with it. Now we are going to create a new collection called &#8220;User&#8221;, so once we made the entry to this collection, the database will also be created automatically. Let&#8217;s do that.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; db.users.insert({"name":"Sibees Venu"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise &gt;</pre>
<p>Now if you run the &#8220;show dbs&#8221; command again, the database MongoIndex will show up. If you ever need to see the collections you have in the DB, you just need to run the command &#8220;show collections&#8221;.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; show collections
users
MongoDB Enterprise &gt;</pre>
<p>The MongoDB is very friendly when it comes to data, it doesn&#8217;t require any schema to get it started. The learning is so easy, am I right?</p>
<p>The other benefit of MongoDB is its <a href="http://sibeeshpassion.com/category/javascript/">JavaScript</a> interpreted shell, where we can actually type JavaScript code and run. To test it out, let&#8217;s create a variable and use it.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; var name = "Sibeesh Venu"
MongoDB Enterprise &gt; name
Sibeesh Venu
MongoDB Enterprise &gt;</pre>
<p>This way, we can interact with the database with a JavaScript program. Now let&#8217;s go ahead and creating a collection called &#8220;Numbers&#8221; and insert 26,000 rows in it. So how are we going to do that? Yes, you are right, we are going to write a for loop, the mongo shell gives that kind of flexibility. Let&#8217;s see that in action.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; for(i=0;i&lt;=26000;i++){
... db.Numbers.insert({
... "number":i
... })
... }
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise &gt;</pre>
<p>So we have done that. Note that, we are able to break the commands into multiple lines, this allows to break the complex codes to much readable format in the shell itself. Sounds good?</p>
<p>Even though we have inserted 26,000 rows,  it always shows,<code class="EnlighterJSRAW" data-enlighter-language="shell">"nInserted" : 1</code> this is because it is counting a number of operations, not the individual documents. Let&#8217;s see this by checking the count now.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; db.numbers.count()
0
MongoDB Enterprise &gt; db.Numbers.count()
26001
MongoDB Enterprise &gt;</pre>
<p>Please note that it is case sensitive.</p>
<h3>Indexes in MongoDB</h3>
<p>Now if you need to see any particular record,  you can always write the query in the shell as follows.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; db.Numbers.find(
... {"number":24000}
... )
{ "_id" : ObjectId("5a8d3be2020a0071d115cf62"), "number" : 24000 }
MongoDB Enterprise &gt;</pre>
<p>So in the query, we are using the function &#8220;find&#8221; with the filter &#8220;number: 24000&#8221;, so that the Mongo can return the record which has the number value as 24000. Now that we have got the output we needed, would you like to see what just happened in the background? To do so, we can use the function &#8220;explain()&#8221;.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; db.Numbers.find( {"number":24000} ).explain()
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "MongoIndex.Numbers",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "number" : {
                                "$eq" : 24000
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "number" : {
                                        "$eq" : 24000
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "serverInfo" : {
                "host" : "PC292716",
                "port" : 27017,
                "version" : "3.4.9",
                "gitVersion" : "876ebee8c7dd0e2d992f36a848ff4dc50ee6603e"
        },
        "ok" : 1
}
MongoDB Enterprise &gt;</pre>
<p>And, if you need to get more information about the execution, you can pass the parameter &#8220;executionStats&#8221; to the &#8220;explain&#8221; function.</p>
<blockquote><p>The parameter is always case sensitive, you will get an errors as below, if you give it wrong. So please make sure you are passing executionStats not executionstats.</p>
<p>&#8220;MongoDB Enterprise &gt; db.Numbers.find( {&#8220;number&#8221;:24000} ).explain(&#8220;executionstats&#8221;)<br />
2018-02-21T15:12:34.197+0530 E QUERY [thread1] Error: explain verbosity must be one of {&#8216;queryPlanner&#8217;,&#8217;executionStats&#8217;,&#8217;allPlansExecution&#8217;} :<br />
parseVerbosity@src/mongo/shell/explainable.js:22:1<br />
constructor@src/mongo/shell/explain_query.js:83:27<br />
DBQuery.prototype.explain@src/mongo/shell/query.js:520:24<br />
@(shell):1:1&#8243;</p></blockquote>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; db.Numbers.find( {"number":24000} ).explain("executionStats")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "MongoIndex.Numbers",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "number" : {
                                "$eq" : 24000
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "number" : {
                                        "$eq" : 24000
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 13,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 26001,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "number" : {
                                        "$eq" : 24000
                                }
                        },
                        "nReturned" : 1,
                        "executionTimeMillisEstimate" : 11,
                        "works" : 26003,
                        "advanced" : 1,
                        "needTime" : 26001,
                        "needYield" : 0,
                        "saveState" : 203,
                        "restoreState" : 203,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "direction" : "forward",
                        "docsExamined" : 26001
                }
        },
        "serverInfo" : {
                "host" : "PC292716",
                "port" : 27017,
                "version" : "3.4.9",
                "gitVersion" : "876ebee8c7dd0e2d992f36a848ff4dc50ee6603e"
        },
        "ok" : 1
}
MongoDB Enterprise &gt;</pre>
<p>Now you can see more information on the execution of how much time it took for the execution and how many docs it is examined etc. If you have noticed, it has examined all the 26001 records and took 13 milliseconds. That&#8217;s just a case, that we had only less number of records in the table, what if, we have millions of records in it? And examining all the records would be a bad idea, am I right? So what do we do at that time? What would be a permanent solution for this? This is where the importance of <em>Indexes</em> comes into action.</p>
<p>Let&#8217;s create an Index for the number that we are going to search.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; db.Numbers.createIndex({number:1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
MongoDB Enterprise &gt;</pre>
<p>Here the number is a special variable, not a string. As you can see, we had created the index. You can see that the property value of createdCollectionAutomatically is false, as the collection had already created and it didn&#8217;t have to create it again.</p>
<p>Let&#8217;s run our find query again.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">MongoDB Enterprise &gt; db.Numbers.find( {"number":24000} ).explain("executionStats")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "MongoIndex.Numbers",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "number" : {
                                "$eq" : 24000
                        }
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "number" : 1
                                },
                                "indexName" : "number_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "number" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "number" : [
                                                "[24000.0, 24000.0]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [
                        {
                                "stage" : "FETCH",
                                "inputStage" : {
                                        "stage" : "IXSCAN",
                                        "keyPattern" : {
                                                "number" : 24000
                                        },
                                        "indexName" : "number_24000",
                                        "isMultiKey" : false,
                                        "multiKeyPaths" : {
                                                "number" : [ ]
                                        },
                                        "isUnique" : false,
                                        "isSparse" : false,
                                        "isPartial" : false,
                                        "indexVersion" : 2,
                                        "direction" : "forward",
                                        "indexBounds" : {
                                                "number" : [
                                                        "[24000.0, 24000.0]"
                                                ]
                                        }
                                }
                        }
                ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 36,
                "totalKeysExamined" : 1,
                "totalDocsExamined" : 1,
                "executionStages" : {
                        "stage" : "FETCH",
                        "nReturned" : 1,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 3,
                        "advanced" : 1,
                        "needTime" : 0,
                        "needYield" : 0,
                        "saveState" : 1,
                        "restoreState" : 1,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 1,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 1,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 2,
                                "advanced" : 1,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 1,
                                "restoreState" : 1,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                        "number" : 1
                                },
                                "indexName" : "number_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "number" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "number" : [
                                                "[24000.0, 24000.0]"
                                        ]
                                },
                                "keysExamined" : 1,
                                "seeks" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : "PC292716",
                "port" : 27017,
                "version" : "3.4.9",
                "gitVersion" : "876ebee8c7dd0e2d992f36a848ff4dc50ee6603e"
        },
        "ok" : 1
}
MongoDB Enterprise &gt;</pre>
<p>As we had given the index on what exactly we are going to search, it just examined only that document when we run the query, that&#8217;s why the value of the property <em>totalDocsExamined </em>is 1. Indexing will not have many impacts on the database which has few records in it, but it has a massive effect on very large data sets which has millions of records in it. Using this simple Indexes can reduce the execution time to almost nothing.</p>
<p>With that, we are done with this post. I will be posting the continuation part of this series very soon. Till then, bye.</p>
<h2><span id="conclusion">Conclusion</span></h2>
<p>Thanks a lot for reading. Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h2><span id="your-turn-what-do-you-think">Your turn. What do you think?</span></h2>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/learn-mongodb-with-me/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
