

{"id":48289,"date":"2019-01-31T13:50:30","date_gmt":"2019-01-31T08:20:30","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=48289"},"modified":"2021-05-09T13:07:21","modified_gmt":"2021-05-09T07:37:21","slug":"mongodb-covered-query","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/","title":{"rendered":"MongoDB Covered Query &amp; Analyzing Query With Example"},"content":{"rendered":"<p>In the last tutorial, we had seen learned about <strong>MongoDB Text Search<\/strong>. Now we will discuss MongoDB Covered Query and Analyzing Query with Examples.<\/p>\n<p>A covered query is one which uses an index and does not have to examine any documents. In MongoDB analyzing query, we will cover $hint and $explain.<\/p>\n<h2>MongoDB Covered Query<\/h2>\n<p>The MongoDB covered query is one which uses an <strong>index<\/strong> and does not have to examine any documents. An index will cover a query if it satisfies the following conditions:<\/p>\n<ul>\n<li>All fields in a query are part of an index.<\/li>\n<li>All fields returned in the results are of the same index.<\/li>\n<\/ul>\n<p>If we take an example of a <strong>collection<\/strong> named example which has 2 indexes type and item.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">db.example.createIndex( { type: 1, item: 1 } )<\/pre>\n<p>This index will cover queries with operations on type and item fields and then return only item field.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">db.example.find(\r\n{ type: \"game\", item:\/^c\/ },\r\n{ item: 1, _id: 0 }\r\n)<\/pre>\n<p>Here, to cover the query, the <strong>document<\/strong> must explicitly specify _id: 0 to exclude the _id field from a result. However, this has changed in version 3.6. Over here an index can cover a query on fields within embedded documents.<\/p>\n<h4>Example &#8211; 1<\/h4>\n<p>Take an example of a collection named tutorials with documents of the following form:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{ _id: 1, user: { login: \"student\" } }<\/pre>\n<p>The collection is having the following index:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{ \"user.login\": 1 }<\/pre>\n<p>The above index will cover the following MongoDB covered query:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">db.tutorial.find( { \"user.login\": \"student\" }, { \"user.login\": 1, _id: 0 } )<\/pre>\n<h4>Example &#8211; 2<\/h4>\n<p>Now, we will take another example to understand it in a more better way.<\/p>\n<p>Consider a document in examples collection.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{\r\n\"_id\": ObjectId(\"53402597d852426020000002\"),\r\n\"contact\": \"1234567809\",\r\n\"dob\": \"01-01-1991\",\r\n\"gender\": \"M\",\r\n\"name\": \"ABC\",\r\n\"user_name\": \"abcuser\"\r\n}<\/pre>\n<p>We will create a compound index for the examples collection on the fields gender and user_name.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;db.examples.ensureIndex({gender:1,user_name:1})<\/pre>\n<p>Now it will cover the following query:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;db.examples.find({gender:\"M\"},{user_name:1,_id:0})<\/pre>\n<p>From the above examples, we can say that MongoDB will not look into database documents but it will fetch the required data from indexed data which will make it very fast and efficient. An index cannot cover a query if:<\/p>\n<ul>\n<li>Any indexed field is an array.<\/li>\n<li>Any indexed field is a subdocument.<\/li>\n<\/ul>\n<h2>MongoDB Analyzing Query<\/h2>\n<p>This comes in the picture because it helps in measuring how effective the database and design is. We will look into <strong>$explain<\/strong> and $<strong>hint<\/strong> which are frequently used queries.<\/p>\n<h3>i. $explain<\/h3>\n<p>It provides information on the query, indexes used in the query and some statistics. It is good to use this if you want to know how well your indexes are optimized.<\/p>\n<p>Now we will take an example to understand about this query. We will use the collection named examples.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{\r\n\"_id\": ObjectId(\"53402597d852426020000002\"),\r\n\"contact\": \"1234567809\",\r\n\"dob\": \"01-01-1991\",\r\n\"gender\": \"M\",\r\n\"name\": \"ABC\",\r\n\"user_name\": \"abcuser\"\r\n}\r\n&gt;db.examples.ensureIndex({gender:1,user_name:1})<\/pre>\n<p>Now we will use $explain query over it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;db.examples.find({gender:\"M\"},{user_name:1,_id:0}).explain()<\/pre>\n<p>After executing the above line we will get the following output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{\r\n  \"cursor\" : \"BtreeCursor gender_1_user_name_1\",\r\n  \"isMultiKey\" : false,\r\n  \"n\" : 1,\r\n  \"nscannedObjects\" : 0,\r\n  \"nscanned\" : 1,\r\n  \"nscannedObjectsAllPlans\" : 0,\r\n  \"nscannedAllPlans\" : 1,\r\n  \"scanAndOrder\" : false,\r\n  \"indexOnly\" : true,\r\n  \"nYields\" : 0,\r\n  \"nChunkSkips\" : 0,\r\n  \"millis\" : 0,\r\n  \"indexBounds\" : {\r\n  \"gender\" : [\r\n    [\r\n      \"M\",\r\n      \"M\"\r\n    ]\r\n  ],\r\n  \"user_name\" : [\r\n    [\r\n      {\r\n        \"$minElement\" : 1\r\n      },\r\n      {\r\n        \"$maxElement\" : 1\r\n       }\r\n      ]\r\n    ]\r\n  }\r\n}<\/pre>\n<p>Now we will understand the following output:<\/p>\n<ul>\n<li>The true value of indexOnly indicates that is has used indexing.<\/li>\n<li>Cursor field specifies the type of cursor used. BTreeCursor type indicates that we have used the index and also gives the name to it. BasicCursor tells us that a full scan was made without using any indexes.<\/li>\n<li>n indicates the number of documents matched.<\/li>\n<li>nscannedObjects tells the total number of documents scanned.<\/li>\n<li>nscanned tells us the total number of documents or index entries scanned.<\/li>\n<\/ul>\n<h3>ii. $hint<\/h3>\n<p>This operator forces the query optimizer to use the specified index to run a single query. This is beneficial when you want to test the performance of a query with different indexes. We will take the above example only with collection name as \u201cexamples\u201d.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;db.examples.find({gender:\"M\"},{user_name:1,_id:0}).hint({gender:1,user_name:1})<\/pre>\n<p>So, this was all about\u00a0MongoDB Covered Query and Analyzing Query. Hope, you liked our explanation.<\/p>\n<h2>Summary<\/h2>\n<p>Hence, we have studied <strong>MongoDB<\/strong> covered query and analyzing query with their examples. In addition, we learned\u00a0$explain and $hint operator with examples. Still, have a doubt? Feel free to ask in the comment section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last tutorial, we had seen learned about MongoDB Text Search. Now we will discuss MongoDB Covered Query and Analyzing Query with Examples. A covered query is one which uses an index and&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":48302,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[18621,18622,18620,18618,18619,18617],"class_list":["post-48289","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","tag-explain","tag-hint","tag-analyzing-query-in-mongodb","tag-covered-query-in-mongodb","tag-mongodb-analyzing-query","tag-mongodb-covered-query"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MongoDB Covered Query &amp; Analyzing Query With Example - DataFlair<\/title>\n<meta name=\"description\" content=\"MongoDB Covered Query Tutorial - What is MongoDB Analyzing Queries With Real-time Example, $explain and $hint operator with syntax\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB Covered Query &amp; Analyzing Query With Example - DataFlair\" \/>\n<meta property=\"og:description\" content=\"MongoDB Covered Query Tutorial - What is MongoDB Analyzing Queries With Real-time Example, $explain and $hint operator with syntax\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-31T08:20:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-09T07:37:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/01\/MongoDB-Query-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MongoDB Covered Query &amp; Analyzing Query With Example - DataFlair","description":"MongoDB Covered Query Tutorial - What is MongoDB Analyzing Queries With Real-time Example, $explain and $hint operator with syntax","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:\/\/data-flair.training\/blogs\/mongodb-covered-query\/","og_locale":"en_US","og_type":"article","og_title":"MongoDB Covered Query &amp; Analyzing Query With Example - DataFlair","og_description":"MongoDB Covered Query Tutorial - What is MongoDB Analyzing Queries With Real-time Example, $explain and $hint operator with syntax","og_url":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-01-31T08:20:30+00:00","article_modified_time":"2021-05-09T07:37:21+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/01\/MongoDB-Query-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"MongoDB Covered Query &amp; Analyzing Query With Example","datePublished":"2019-01-31T08:20:30+00:00","dateModified":"2021-05-09T07:37:21+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/"},"wordCount":598,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/01\/MongoDB-Query-01.jpg","keywords":["$explain","$hint","Analyzing Query in mongoDB","Covered Query in MongoDB","MongoDB Analyzing Query","MongoDB Covered Query"],"articleSection":["MongoDB Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/","url":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/","name":"MongoDB Covered Query &amp; Analyzing Query With Example - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/01\/MongoDB-Query-01.jpg","datePublished":"2019-01-31T08:20:30+00:00","dateModified":"2021-05-09T07:37:21+00:00","description":"MongoDB Covered Query Tutorial - What is MongoDB Analyzing Queries With Real-time Example, $explain and $hint operator with syntax","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/01\/MongoDB-Query-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/01\/MongoDB-Query-01.jpg","width":1200,"height":628,"caption":"MongoDB Covered Query &amp; Analyzing Query With Example"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/mongodb-covered-query\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"MongoDB Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/mongodb\/"},{"@type":"ListItem","position":3,"name":"MongoDB Covered Query &amp; Analyzing Query With Example"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/48289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=48289"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/48289\/revisions"}],"predecessor-version":[{"id":93182,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/48289\/revisions\/93182"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/48302"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=48289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=48289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=48289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}