{"id":55931,"date":"2018-03-07T15:00:29","date_gmt":"2018-03-07T13:00:29","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=55931"},"modified":"2018-03-07T11:17:24","modified_gmt":"2018-03-07T09:17:24","slug":"mongodb-explain-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/","title":{"rendered":"MongoDB explain() Example"},"content":{"rendered":"<p>Hello readers, in today\u2019s world it is important to understand the information of a query plan. In Mongo world, it is pretty straightforward and in this tutorial, we will see the <code>explain()<\/code> method available in the Mongo database.<\/p>\n<h2>1. Introduction<\/h2>\n<p>If you have installed the Mongo application (<em>version 3.6<\/em>) on Windows or Ubuntu operating system and you wish to learn the <code>explain()<\/code> method then follow the below steps. It is very simple, but before moving further let&#8217;s take a look at the Mongo database and its characteristics.<\/p>\n<h3>1.1 What is MongoDB?<\/h3>\n<ul>\n<li>MongoDB is a high-performance <em>NoSQL database<\/em> where each database has collections which in turn has documents. Each document has a different number of fields, size, content, and is stored in a JSON-like format (i.e. Binary JSON (<a href=\"https:\/\/en.wikipedia.org\/wiki\/BSON\" target=\"_blank\" rel=\"noopener\">BSN<\/a>))<\/li>\n<li>The documents in MongoDB doesn\u2019t need to have a schema defined beforehand. Instead, the fields (i.e. <em>records<\/em>) can be created on the go<\/li>\n<li>Data model available within the MongoDB allows developers to represent the hierarchical relationships, store arrays, and other more complex structures easily<\/li>\n<li>This NoSQL solution often comes with embedding, auto-sharding, and onboard replication for better scalability and high availability<\/li>\n<\/ul>\n<h4>1.1.1 Why MongoDB?<\/h4>\n<ul>\n<li>As a NoSQL type database, MongoDB stores the data in the form of a document. Thus, MongoDB offers more flexibility<\/li>\n<li>This database supports search by field-name, range queries, and the regular expressions. It often provides queries to return the particular fields inside the documents<\/li>\n<li>MongoDB offers <em>indexes<\/em> to improve the <em>search performance<\/em> within the NoSQL database<\/li>\n<li>To offer horizontal scalability, MongoDB uses sharding by splitting the data across the many MongoDB occurrences<\/li>\n<li><strong>Replication<\/strong>: MongoDB can give high availability with the replica sets<\/li>\n<\/ul>\n<h3>1.2 What is a Cursor in MongoDB?<\/h3>\n<p>In Mongo world, a <strong>cursor<\/strong> is an object that allows developers to iterate through the documents of a Mongo collection. The behavior of cursor allows an automatic iteration across the results of the query; however, developers can explicitly go through the items returned in the cursor object. The below diagram lists <code>4<\/code> documents where the Mongo <em>cursor<\/em> will point to the first document and then iterate through all the other documents of a collection.<\/p>\n<p><figure id=\"attachment_55932\" aria-describedby=\"caption-attachment-55932\" style=\"width: 673px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-archi-guide-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-55932\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-archi-guide-1.jpg\" alt=\"Fig. 1: Pictorial representation of a Cursor in Mongo collection\" width=\"673\" height=\"492\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-archi-guide-1.jpg 673w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-archi-guide-1-300x219.jpg 300w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/a><figcaption id=\"caption-attachment-55932\" class=\"wp-caption-text\">Sample Json Structure<\/figcaption><\/figure><\/p>\n<h4>1.2.1 Why Cursor in MongoDB?<\/h4>\n<p>Cursor offers:<\/p>\n<ul>\n<li>A true snapshot of a system i.e. it returns the data in batches and increases the database performance<\/li>\n<li>It saves system memory by allowing batch inserts and updates<\/li>\n<li>Intelligibility and Clarity on the ad-hoc and complex queries of the sequential nature having large result sets and low consistency requirements<\/li>\n<li>Openness to work on small batches of data as developers don\u2019t need to wait for the processing and download of the complete record-set<\/li>\n<\/ul>\n<h2>2. MongoDB explain() Example<\/h2>\n<p>In this tutorial, we will learn how to handle the <code>explain()<\/code> method provided by the Mongo database.<\/p>\n<h3>2.1 explain() method in the Mongo database<\/h3>\n<p>In the Mongo universe, the <code><a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/method\/cursor.explain\/\" target=\"_blank\" rel=\"noopener\">explain()<\/a><\/code> method returns a document along with the information on the query plan. This method has the following prototype form:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Mongo database &#8216;explain()&#8217; Syntax<\/em><\/span><\/p>\n<pre class=\"brush:bash;wrap-lines:false;\">&gt; db.collection_name.explain(&lt;verbosity_parameter&gt;)\r\n<\/pre>\n<p>Where:<\/p>\n<ul>\n<li>The <strong>verbosity_parameter<\/strong> is an optional string value that specifies the verbosity mode for the explain command. This value determines the amount of the returning information and supports the following <code>3<\/code> modes i.e. <code>queryPlanner<\/code>, <code>executionStats<\/code>, and <code>allPlansExecution<\/code><\/li>\n<\/ul>\n<p>Do <em>remember<\/em>, the default mode for the <code>explain()<\/code> method is the <code>queryPlanner<\/code>.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h4>2.1.1 Behavior of Verbosity modes<\/h4>\n<p>Let\u2019s take a look at the below-listed <span style=\"text-decoration: underline;\">verbosity modes<\/span> to find the amount of the information provided.<\/p>\n<ul>\n<li><strong>queryPlanner<\/strong> Mode: This is the default mode and the specified query in the <code><a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/method\/db.collection.find\/#db.collection.find\" target=\"_blank\" rel=\"noopener\">find()<\/a><\/code> method is put by the query optimizer to find the most efficient plan. The effective plan is then passed to this mode and the information is returned for the evaluated query<\/li>\n<li><strong>executionStats<\/strong> Mode: When a Mongo query is executing in this mode, the query optimizer selects and executes the execution plan to complete and return the statistics describing the winning plan execution<\/li>\n<li><strong>allPlansExecution<\/strong> Mode: When a Mongo query is executing in this mode, the query optimizer selects and executes the execution plan for further processing. In this mode, Mongo database return the statistics describing the winning plan execution and the other candidate plans captured during the plan choice<\/li>\n<\/ul>\n<h3>2.2 Practical usage<\/h3>\n<p>Let&#8217;s understand the implementation of this method with the help of the sample snippet.<\/p>\n<h4>2.2.1 Start MongoDB<\/h4>\n<p>Start a standalone <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/program\/mongod\/#bin.mongod\" target=\"_blank\" rel=\"noopener\">mongod<\/a> instance as shown below.<\/p>\n<p><figure id=\"attachment_55933\" aria-describedby=\"caption-attachment-55933\" style=\"width: 849px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-55933\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-1.jpg\" alt=\"Fig. 2: Start Mongo instance\" width=\"849\" height=\"613\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-1.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-1-300x217.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-1-768x555.jpg 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/a><figcaption id=\"caption-attachment-55933\" class=\"wp-caption-text\">Fig. 2: Start Mongo instance<\/figcaption><\/figure><\/p>\n<h4>2.2.2 Connect to the Mongo Instance<\/h4>\n<p>Connect with the <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/program\/mongo\/#bin.mongo\" target=\"_blank\" rel=\"noopener\">mongo<\/a> shell to make a connection with the MongoDB instance on the port <code>27017<\/code> as shown below.<\/p>\n<p><figure id=\"attachment_55934\" aria-describedby=\"caption-attachment-55934\" style=\"width: 849px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-55934\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-2.jpg\" alt=\"Fig. 3: Connect to Mongo database\" width=\"849\" height=\"362\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-2.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-2-300x128.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-2-768x327.jpg 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/a><figcaption id=\"caption-attachment-55934\" class=\"wp-caption-text\">Fig. 3: Connect to Mongo database<\/figcaption><\/figure><\/p>\n<h4>2.2.3 Create Mongo database and collection<\/h4>\n<p>To begin with the implementation, we will need to create a sample database and collection. The below script creates a database called <code>warehouse<\/code> with a collection of <code>products<\/code>. Open the Mongo terminal and execute the script.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Database &amp; Collection creation script<\/em><\/span><\/p>\n<pre class=\"brush:bash;wrap-lines:false;\">&gt; use warehouse\r\n\r\n&gt; db.products.insertMany( [\r\n\t{ \"_id\" : \"1001\", \"name\" : \"AC3 Phone\", \"brand\" : \"ACME\", \"type\" : \"phone\", \"price\" : 200, \"warranty_years\" : 1, \"available\" : \"true\" },\r\n\t{ \"_id\" : \"1002\", \"name\" : \"AC7 Phone\", \"brand\" : \"ACME\", \"type\" : \"phone\", \"price\" : 320, \"warranty_years\" : 1, \"available\" : \"false\" },\r\n\t{ \"_id\" : \"1003\", \"name\" : \"AC3 Series Charger\", \"type\" : [ \"accessory\", \"charger\" ], \"price\" : 19, \"warranty_years\" : 0.25, \"for\" : [ \"ac3\", \"ac7\", \"ac9\" ] },\r\n\t{ \"_id\" : \"1004\", \"name\" : \"AC3 Case Green\", \"type\" : [ \"accessory\", \"case\" ], \"color\" : \"green\", \"price\" : 12, \"warranty_years\" : 0 },\r\n\t{ \"_id\" : \"1005\", \"name\" : \"Phone Extended Warranty\", \"type\" : \"warranty\", \"price\" : 38, \"warranty_years\" : 2, \"for\" : [ \"ac3\", \"ac7\", \"ac9\", \"qp7\", \"qp8\", \"qp9\" ] },\r\n\t{ \"_id\" : \"1006\", \"name\" : \"AC3 Case Black\", \"type\" : [ \"accessory\", \"case\" ], \"color\" : \"black\", \"price\" : 12.5, \"warranty_years\" : 0.25, \"available\" : \"false\", \"for\" : \"ac3\" },\r\n\t{ \"_id\" : \"1007\", \"name\" : \"AC3 Case Red\", \"type\" : [ \"accessory\", \"case\" ], \"color\" : \"red\", \"price\" : 12, \"warranty_years\" : 0.25, \"available\" : \"true\", \"for\" : \"ac3\" },\r\n\t{ \"_id\" : \"1008\", \"name\" : \"Phone Service Basic Plan\", \"type\" : \"service\", \"monthly_price\" : 40, \"limits\" : { \"voice\" : { \"units\" : \"minutes\", \"n\" : 400, \"over_rate\" : 0.05 }, \"data\" : { \"units\" : \"gigabytes\", \"n\" : 20, \"over_rate\" : 1 }, \"sms\" : { \"units\" : \"texts sent\", \"n\" : 100, \"over_rate\" : 0.001 } }, \"term_years\" : 2 },\r\n\t{ \"_id\" : \"1009\", \"name\" : \"Phone Service Core Plan\", \"type\" : \"service\", \"monthly_price\" : 60, \"limits\" : { \"voice\" : { \"units\" : \"minutes\", \"n\" : 1000, \"over_rate\" : 0.05 }, \"data\" : { \"n\" : \"unlimited\", \"over_rate\" : 0 }, \"sms\" : { \"n\" : \"unlimited\", \"over_rate\" : 0 } }, \"term_years\" : 1 },\r\n\t{ \"_id\" : \"1010\", \"name\" : \"Phone Service Family Plan\", \"type\" : \"service\", \"monthly_price\" : 90, \"limits\" : { \"voice\" : { \"units\" : \"minutes\", \"n\" : 1200, \"over_rate\" : 0.05 }, \"data\" : { \"n\" : \"unlimited\", \"over_rate\" : 0 }, \"sms\" : { \"n\" : \"unlimited\", \"over_rate\" : 0 } }, \"sales_tax\" : true, \"term_years\" : 2 },\r\n\t{ \"_id\" : \"1011\", \"name\" : \"Cable TV Basic Service Package\", \"type\" : \"tv\", \"monthly_price\" : 50, \"term_years\" : 2, \"cancel_penalty\" : 25, \"sales_tax\" : \"true\", \"additional_tariffs\" : [ { \"kind\" : \"federal tariff\", \"amount\" : { \"percent_of_service\" : 0.06 } }, { \"kind\" : \"misc. tariff\", \"amount\" : 2.25 } ] }\r\n] )\r\n<\/pre>\n<p>The script gives the below output.<\/p>\n<p><figure id=\"attachment_55935\" aria-describedby=\"caption-attachment-55935\" style=\"width: 849px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-3.jpg\"><img decoding=\"async\" class=\"size-full wp-image-55935\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-3.jpg\" alt=\"Fig. 4: Database &amp; Collection creation\" width=\"849\" height=\"357\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-3.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-3-300x126.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-3-768x323.jpg 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/a><figcaption id=\"caption-attachment-55935\" class=\"wp-caption-text\">Fig. 4: Database &amp; Collection creation<\/figcaption><\/figure><\/p>\n<h4>2.2.4 Check Mongo database and collection<\/h4>\n<p>If the script works well, the database and the collection will be shown in the Mongo Workbench. Using the <code>db.collection_name.find()<\/code> or the <code>db.collection_name.find().pretty()<\/code> command the documents of a collection will be shown as below.<\/p>\n<p><figure id=\"attachment_55936\" aria-describedby=\"caption-attachment-55936\" style=\"width: 849px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-4.jpg\"><img decoding=\"async\" class=\"size-full wp-image-55936\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-4.jpg\" alt=\"Fig. 5: Mongo database &amp; collection\" width=\"849\" height=\"179\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-4.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-4-300x63.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-4-768x162.jpg 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/a><figcaption id=\"caption-attachment-55936\" class=\"wp-caption-text\">Fig. 5: Mongo database &amp; collection<\/figcaption><\/figure><\/p>\n<h4>2.2.5 Implementation of &#8216;explain()&#8217; method<\/h4>\n<p>Now, go back to the Mongo shell and use the <code>explain()<\/code> method to display the query planning information for the specified <code>find()<\/code> operation.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Query 1<\/em><\/span><\/p>\n<pre class=\"brush:bash;wrap-lines:false;\">&gt; db.products.find( { \"available\" : \"true\" } ).explain()\r\n<\/pre>\n<p>This command will execute in the default verbosity mode (i.e. &#8220;queryPlanner&#8221;) to return the query planning information.<\/p>\n<p><figure id=\"attachment_55937\" aria-describedby=\"caption-attachment-55937\" style=\"width: 849px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-5.jpg\"><img decoding=\"async\" class=\"size-full wp-image-55937\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-5.jpg\" alt=\"Fig. 6: Mongo database explain() method\" width=\"849\" height=\"268\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-5.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-5-300x95.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-5-768x242.jpg 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/a><figcaption id=\"caption-attachment-55937\" class=\"wp-caption-text\">Fig. 6: Mongo database explain() method<\/figcaption><\/figure><\/p>\n<p>In Fig. 6, the <code>queryPlanner<\/code> section is worth exploring i.e.<\/p>\n<ul>\n<li>The <code>queryStage<\/code> attribute provides information about the &#8220;execution plan&#8221; or &#8220;winning plan&#8221; operation type. This can be one of the following:\n<ul>\n<li>The <code>COLLSCAN<\/code> value indicates a full collection scan i.e. To fetch the result, the specified query searched each document in the Mongo collection<\/li>\n<li>The <code>IXSCAN<\/code> value indicates an index search<\/li>\n<li>The <code>FETCH<\/code> value is simply used for retrieving the documents of a collection<\/li>\n<li>The <code>SHARD_MERGE<\/code> value indicates the merged data from different shards<\/li>\n<\/ul>\n<\/li>\n<li>The <code>direction<\/code> attribute shows whether the query was performed in a forward or reverse order<\/li>\n<li>The <code>serverInfo<\/code> section displays the server information on which the query was executed and the version of the Mongo database. Do <em>remember<\/em>, each accessed shard information would be listed in this section if the collection is in a sharded environment<\/li>\n<li>The <code> \"ok\" : 1<\/code> output means the command is executed successfully<\/li>\n<\/ul>\n<p>Now let\u2019s say developers want to execute the query using the <code>executionStats<\/code> verbosity mode. This mode will display more information and has the following prototype form:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Query 2<\/em><\/span><\/p>\n<pre class=\"brush:bash;wrap-lines:false;\">&gt; db.products.find( { \"available\" : \"true\" } ).explain(\"executionStats\")\r\n<\/pre>\n<p>This command will execute in the <code>executionStats<\/code> verbosity mode to share any more information along with the query planning information.<\/p>\n<p><figure id=\"attachment_55938\" aria-describedby=\"caption-attachment-55938\" style=\"width: 849px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-6.jpg\"><img decoding=\"async\" class=\"size-full wp-image-55938\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-6.jpg\" alt=\"Fig. 7: Mongo database explain() method with &quot;executionStats&quot; verbosity mode\" width=\"849\" height=\"463\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-6.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-6-300x164.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mongodb-explain-project-guide-6-768x419.jpg 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/a><figcaption id=\"caption-attachment-55938\" class=\"wp-caption-text\">Fig. 7: Mongo database explain() method with &#8220;executionStats&#8221; verbosity mode<\/figcaption><\/figure><\/p>\n<p>Among other things, here we see:<\/p>\n<ul>\n<li>The time of the query<\/li>\n<li>The count of the documents that were returned by the query (i.e. <code>nReturned<\/code>)<\/li>\n<li>Total number of the documents that were examined by the database (i.e. <code>totalDocsExamined<\/code>)<\/li>\n<\/ul>\n<p>That\u2019s all for this post. Happy Learning!!<\/p>\n<h2>3. Conclusion<\/h2>\n<p>In this tutorial, we learned about the <code>explain()<\/code> method that can be used by the programmers to check the slow queries in the Mongo database. Developers can download the sample commands in the <a href=\"#projectDownload\">Downloads<\/a> section.<\/p>\n<h2><a name=\"projectDownload\"><\/a>4. Download the Eclipse Project<\/h2>\n<p>This was an example of the <code>explain()<\/code> method available in the Mongo database.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/CodeSnippet.zip\" target=\"_blank\" rel=\"noopener\"><strong>CodeSnippet<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello readers, in today\u2019s world it is important to understand the information of a query plan. In Mongo world, it is pretty straightforward and in this tutorial, we will see the explain() method available in the Mongo database. 1. Introduction If you have installed the Mongo application (version 3.6) on Windows or Ubuntu operating system &hellip;<\/p>\n","protected":false},"author":119,"featured_media":36154,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1447],"tags":[1194,1708],"class_list":["post-55931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","tag-mongodb","tag-mongodb-cursor-methods"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MongoDB explain() Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Hello readers, in this tutorial, we will see the explain() method available in the Mongo database.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB explain() Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Hello readers, in this tutorial, we will see the explain() method available in the Mongo database.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-07T13:00:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yatin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yatin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/\"},\"author\":{\"name\":\"Yatin\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\"},\"headline\":\"MongoDB explain() Example\",\"datePublished\":\"2018-03-07T13:00:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/\"},\"wordCount\":1238,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"keywords\":[\"MongoDb\",\"MongoDB Cursor Methods\"],\"articleSection\":[\"MongoDB\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/\",\"name\":\"MongoDB explain() Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"datePublished\":\"2018-03-07T13:00:29+00:00\",\"description\":\"Hello readers, in this tutorial, we will see the explain() method available in the Mongo database.\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Software Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/software-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MongoDB\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/software-development\/mongodb\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"MongoDB explain() Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\",\"name\":\"Yatin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"caption\":\"Yatin\"},\"description\":\"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).\",\"sameAs\":[\"https:\/\/www.javacodegeeks.com\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MongoDB explain() Example - Java Code Geeks","description":"Hello readers, in this tutorial, we will see the explain() method available in the Mongo database.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/","og_locale":"en_US","og_type":"article","og_title":"MongoDB explain() Example - Java Code Geeks","og_description":"Hello readers, in this tutorial, we will see the explain() method available in the Mongo database.","og_url":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2018-03-07T13:00:29+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","type":"image\/jpeg"}],"author":"Yatin","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yatin","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/"},"author":{"name":"Yatin","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13"},"headline":"MongoDB explain() Example","datePublished":"2018-03-07T13:00:29+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/"},"wordCount":1238,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","keywords":["MongoDb","MongoDB Cursor Methods"],"articleSection":["MongoDB"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/","url":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/","name":"MongoDB explain() Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","datePublished":"2018-03-07T13:00:29+00:00","description":"Hello readers, in this tutorial, we will see the explain() method available in the Mongo database.","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-explain-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Software Development","item":"https:\/\/examples.javacodegeeks.com\/category\/software-development\/"},{"@type":"ListItem","position":3,"name":"MongoDB","item":"https:\/\/examples.javacodegeeks.com\/category\/software-development\/mongodb\/"},{"@type":"ListItem","position":4,"name":"MongoDB explain() Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13","name":"Yatin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","caption":"Yatin"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/55931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/119"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=55931"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/55931\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/36154"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=55931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=55931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=55931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}