

{"id":14968,"date":"2018-05-14T03:48:33","date_gmt":"2018-05-14T03:48:33","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=14968"},"modified":"2021-05-09T13:07:28","modified_gmt":"2021-05-09T07:37:28","slug":"mongodb-index","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/mongodb-index\/","title":{"rendered":"MongoDB Index: Types, Properties &amp; Limitations"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">In our last MongoDB tutorial, we have discussed <strong>MongoDB Data Types<\/strong>.\u00a0So, let\u2019s move further with MongoDB. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this article, we will discuss MongoDB index, advanced indexing and indexing limitation in MongoDB. Moreover, we are going to learn MongoDB properties.<\/span><br \/>\nSo, let&#8217;s begin with MongoDB Index.<\/p>\n<div id=\"attachment_15832\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-15832\" class=\"wp-image-15832 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index.jpg\" alt=\"MongoDB Index: Types, Properties &amp; Limitations\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-15832\" class=\"wp-caption-text\">MongoDB Index: Types, Properties &amp; Limitations<\/p><\/div>\n<h2>What is MongoDB Index?<\/h2>\n<p><span style=\"font-weight: 400;\">For any kind of database, indexes are of great importance. In MongoDB, Indexes helps to solve queries more efficiently. Indexes are a special data structure used to locate the record in the given table very quickly without being required to traverse through every record in the table. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">MongoDB uses these indexes to limit the number of documents that had to be searched in a collection. The data structure that is used by an index is a Binary Tree.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">a.\u00a0<\/span>Default Index<\/h3>\n<p><span style=\"font-weight: 400;\">In MongoDB indexing, all the collections have a default index on the _id field. If we don\u2019t specify any value for the _id the MongoDB will create _id field with an object value. This index prevents clients from creating two documents with the same value _id field.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">b.\u00a0<\/span>Create an Index<\/h3>\n<p><span style=\"font-weight: 400;\">In MongoDB, a user can create indexes using the following <strong>syntax.<\/strong><\/span><\/p>\n<pre class=\"EnlighterJSRAW\">Db.collection_name.createIndex( &lt;key and index type specification&gt;, &lt;options&gt;)<\/pre>\n<p><span style=\"font-weight: 400;\">This method creates an index only if the index with the same specification doesn\u2019t exist already.<\/span><br \/>\n<span style=\"font-weight: 400;\">Let\u2019s see an example that creates a single key ascending index on the name field.<\/span><br \/>\n<span style=\"font-weight: 400;\">We see a message that \u201cok\u201d:1. That means an index has been created successfully.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt; db.dataflair1.createIndex({name:1})\r\n{\r\n           \u201ccreatedCollectionAutomatically\u201d : false,\r\n            \u201cnumIndexesBefore\u201d : 1,\r\n            \u201cnumIndexesAfter\u201d : 2,\r\n            \u201cok\u201d : 1\r\n}\r\n&gt;<\/pre>\n<div id=\"attachment_14989\" style=\"width: 584px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/index-1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-14989\" class=\"wp-image-14989 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/index-1.jpg\" alt=\"MongoDB Index: Create an Index Method\" width=\"574\" height=\"172\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/index-1.jpg 574w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/index-1-150x45.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/index-1-300x90.jpg 300w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><\/a><p id=\"caption-attachment-14989\" class=\"wp-caption-text\">MongoDB Index: Create an Index Method<\/p><\/div>\n<h2>Types of Index in MongoDB<\/h2>\n<p><span style=\"font-weight: 400;\">MongoDB supports the following types of the index for running a query. <\/span><\/p>\n<h4><span style=\"font-weight: 400;\"><strong>i.<\/strong>\u00a0<\/span>Single Field Index<\/h4>\n<p><span style=\"font-weight: 400;\">MongoDB supports user-defined indexes like single field index. A single field index is used to create an index on the single field of a document. With single field index, MongoDB can traverse in ascending and descending order. That\u2019s why the index key does not matter in this case.<\/span><\/p>\n<h4><strong>ii.\u00a0<\/strong>Compound Index<\/h4>\n<p><span style=\"font-weight: 400;\">MongoDB supports a user-defined index on multiple fields as well. For this MongoDB has a compound index. There sequential order of fields for a compound index.<\/span><\/p>\n<p>For example, if a compound index consists of {\u201cname\u201d:1,\u201dcity\u201d:1}), then the index will sort first the name and then the city.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt; db.dataflair1.find().sort({\u201cname\u201d:1,\u201dcity\u201d:1})\r\n{ \u201c_id\u201d : ObjectId(\u201cSadfeb8931cffc950c924471\u201d), \u201cname\u201d : \u201camit\u201d, \u201ccity\u201d : \u201cujjain\u201d }\r\n{ \u201c_id\u201d : ObjectId(\u201cSadfeb6e31cffc950c92446f\u201d), \u201cname\u201d : \u201cankitt\u201d, \u201ccity\u201d : \u201cindore\u201d }\r\n{ \u201c_id\u201d : ObjectId(\u201cSadfeb7c31cffc950c924470\u201d), \u201cname\u201d : \u201crohit\u201d, \u201ccity\u201d : \u201cujjain\u201d }\r\n{ \u201c_id\u201d : ObjectId(\u201cSadfeb9731cffc950c924472\u201d), \u201cname\u201d : \u201csumit\u201d, \u201ccity\u201d : \u201cdevas\u201d }\r\n&gt;<\/pre>\n<div id=\"attachment_14990\" style=\"width: 763px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/index1-1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-14990\" class=\"wp-image-14990 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/index1-1.jpg\" alt=\"MongoDB Index- Compound Index\" width=\"753\" height=\"155\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/index1-1.jpg 753w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/index1-1-150x31.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/index1-1-300x62.jpg 300w\" sizes=\"auto, (max-width: 753px) 100vw, 753px\" \/><\/a><p id=\"caption-attachment-14990\" class=\"wp-caption-text\">MongoDB Index- Compound Index<\/p><\/div>\n<h4><span style=\"font-weight: 400;\"><strong>iii.<\/strong>\u00a0<\/span><strong>Multikey Index<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">MongoDB uses the multikey indexes to index the values stored in arrays. If we index a field with an array value, MongoDB creates separate index entries for each element of the array. These indexes allow queries to select documents with the matching criteria. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">MongoDB automatically determines whether to create a multikey index if the indexed field contains an array value. We do not need to specify the multikey type explicitly. Below is the example of a multikey index.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt; db.dataflair1.find().sort({\u201cname\u201d:1,\u201dcity\u201d:1})\r\n{ \u201c_id\u201d : ObjectId(\u201cSadfeb6e31cffc950c92446f\u201d), \u201cname\u201d : \u201cankitt\u201d, \u201ccity\u201d : \u201cindore\u201d }\r\n{ \u201c_id\u201d : ObjectId(\u201cSadfeb7c31cffc950c924470\u201d), \u201cname\u201d : \u201crohit\u201d, \u201ccity\u201d : \u201cujjain\u201d }\r\n{ \u201c_id\u201d : ObjectId(\u201cSadfeb8931cffc950c924471\u201d), \u201cname\u201d : \u201camit\u201d, \u201ccity\u201d : \u201cujjain\u201d }\r\n{ \u201c_id\u201d : ObjectId(\u201cSadfeb9731cffc950c924472\u201d), \u201cname\u201d : \u201csumit\u201d, \u201ccity\u201d : \u201cdevas\u201d }\r\n&gt;<\/pre>\n<div id=\"attachment_14988\" style=\"width: 763px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/multifield-1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-14988\" class=\"wp-image-14988 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/multifield-1.jpg\" alt=\"MongoDB Index - Multikey Index\" width=\"753\" height=\"155\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/multifield-1.jpg 753w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/multifield-1-150x31.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/multifield-1-300x62.jpg 300w\" sizes=\"auto, (max-width: 753px) 100vw, 753px\" \/><\/a><p id=\"caption-attachment-14988\" class=\"wp-caption-text\">MongoDB Index &#8211; Multikey Index<\/p><\/div>\n<h4><strong>iv.\u00a0Geospatial Index<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">To query geospatial data, MongoDB supports two types of indexes \u2013 2d indexes and 2d sphere indexes. <\/span><span style=\"font-weight: 400;\">2d indexes<\/span><span style=\"font-weight: 400;\"> use planar geometry when returning results and <\/span><span style=\"font-weight: 400;\">2dsphere indexes<\/span><span style=\"font-weight: 400;\"> use spherical geometry to return results.<\/span><\/p>\n<h4><span style=\"font-weight: 400;\"><strong>v.<\/strong>\u00a0<\/span>Text Index<\/h4>\n<p><span style=\"font-weight: 400;\">It is another type of index that is supported by MongoDB. Text index supports searching for string content in a collection. These index types do not store language-specific\u00a0stop\u00a0words (e.g. &#8220;the&#8221;, &#8220;a&#8221;, &#8220;or&#8221;). Text indexes\u00a0restrict the words in a collection to only store root words. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Below is the example of text index.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">H<\/span><span style=\"font-weight: 400;\">ere is the <strong>synta<\/strong>x we use for text Index<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">db.collection_name.createIndex({name:\u201dtext\u201d})<\/pre>\n<pre class=\"EnlighterJSRAW\">&gt; db.dataflair.createIndex({name:\u201dtext\u201d})\r\n{\r\n         \u201ccreatedCollectionAutomatically\u201d : false,\r\n            \u201cnumIndexesBefore\u201d : 1,\r\n            \u201cnumIndexesAfter\u201d : 2,\r\n            \u201cok\u201d : 1\r\n}<\/pre>\n<div id=\"attachment_14987\" style=\"width: 764px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/text.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-14987\" class=\"wp-image-14987 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/text.jpg\" alt=\"MongoDB Index- Text Index\" width=\"754\" height=\"153\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/text.jpg 754w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/text-150x30.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/text-300x61.jpg 300w\" sizes=\"auto, (max-width: 754px) 100vw, 754px\" \/><\/a><p id=\"caption-attachment-14987\" class=\"wp-caption-text\">MongoDB Index- Text Index<\/p><\/div>\n<h4><strong>vi.<\/strong><i><span style=\"font-weight: 400;\">\u00a0<\/span><\/i>Hashed Index<\/h4>\n<p><span style=\"font-weight: 400;\">MongoDB supports hash-based sharding and provides hashed indexes. These indexes are the hashes of the field value.<\/span><\/p>\n<h2>MongoDB Index Properties<\/h2>\n<h3>a. Unique Indexes<\/h3>\n<p><span style=\"font-weight: 400;\">This property of index causes MongoDB to reject duplicate values for the indexed field. In other words, a unique property of indexes restricts it to insert the duplicate value of an indexed field. The unique indexes can be interchanged functionally with other MongoDB indexes.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s see an example.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We create an index using createIndex() for the name field and set unique to be true. Then insert a document with fields \u201cname\u201d and \u201ccity\u201d.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt; db.dataflair.createIndex({name:1},{unique:true})\r\n{\r\n         \u201ccreatedCollectionAutomatically\u201d : false,\r\n           \u201cnumIndexesBefore\u201d : 2,\r\n           \u201cnumIndexesAfter\u201d : 3,\r\n            \u201cok\u201d : 1\r\n}\r\n&gt; db.data-flair.insert({\u201cname\u201d:\u201dankit\u201d,\u201dcity\u201d:\u201dIndore\u201d})\r\nWriteResult({ \u201cnInserted\u201d : 1 })\r\n&gt; db.data-flair.insert({\u201cname\u201d:\u201dankit\u201d,\u201dcity\u201d:\u201dIndore\u201d})\r\nWriteResult({\r\n                \u201cnInserted\u201d : 0,\r\n                \u201cwriterError\u201d : {\r\n                           \u201ccode\u201d : 11000,\r\n                           \u201cErrmsg\u201d : \u201cE1100 duplicate key error collection: test.dataflair index: name_1 dup key: ( : \\\u201dankit\\\u201d }\u201d\r\n}\r\n})\r\n&gt;<\/pre>\n<div id=\"attachment_14983\" style=\"width: 1008px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/unique.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-14983\" class=\"wp-image-14983 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/unique.jpg\" alt=\"MongoDB Index- Unique Property\" width=\"998\" height=\"340\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/unique.jpg 998w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/unique-150x51.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/unique-300x102.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/unique-768x262.jpg 768w\" sizes=\"auto, (max-width: 998px) 100vw, 998px\" \/><\/a><p id=\"caption-attachment-14983\" class=\"wp-caption-text\">MongoDB Index- Unique Property<\/p><\/div>\n<p><span style=\"font-weight: 400;\">We see that when we try to insert a document again with the same field value with the unique property, an error is displayed.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">So, we cannot insert duplicate value<\/span><span style=\"font-weight: 400;\">s in a field with the unique property.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">b.\u00a0<\/span>Partial Indexes<\/h3>\n<p><span style=\"font-weight: 400;\">This property came in MongoDB version 3.2. Partial Indexes only index the documents that match the filter criteria. If we are creating an index with some conditions applied then it is a partial index.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s create an index on the city field only with the city Indore. For this, we have to use partialFilterExpression.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt; db.dataflair.createIndex({city:1},{partialFilterExpression:{name:\u201dankit\u201d}})\r\n{\r\n         \u201ccreatedCollectionAutomatically\u201d : false,\r\n          \u201cnumIndexesBefore\u201d : 3,\r\n          \u201cnumIndexesAfter\u201d : 4,\r\n           \u201cok\u201d : 1\r\n}\r\n&gt;<\/pre>\n<div id=\"attachment_14984\" style=\"width: 696px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/partial-1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-14984\" class=\"wp-image-14984 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/partial-1.jpg\" alt=\"MongoDB Index- Partial Indexes\" width=\"686\" height=\"185\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/partial-1.jpg 686w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/partial-1-150x40.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/partial-1-300x81.jpg 300w\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" \/><\/a><p id=\"caption-attachment-14984\" class=\"wp-caption-text\">MongoDB Index- Partial Indexes<\/p><\/div>\n<p><span style=\"font-weight: 400;\"> A message displayed as \u201cok\u201d: \u00a01 means that the partial index has been created.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">c.\u00a0<\/span>Sparse Indexes<\/h3>\n<p><span style=\"font-weight: 400;\">The sparse property ensures that the index only contains entries for documents with the indexed field. The index will skip the documents without the indexed field.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We can combine this option with the unique index option in order to reject documents with duplicate values for a field. And can ignore documents without an indexed key at the same time.<\/span><br \/>\n<span style=\"font-weight: 400;\">Example:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt; db.dataflair.createIndex({name:1},{unique:true,sparese:true})\r\n{\r\n          \u201ccreatedCollectionAutomatically\u201d : false,\r\n          \u201cnumIndexesBefore\u201d : 2,\r\n          \u201cnumIndexesAfter\u201d : 3,\r\n           \u201cok\u201d : 1\r\n}\r\n&gt;<\/pre>\n<div id=\"attachment_14985\" style=\"width: 594px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/sparse-1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-14985\" class=\"wp-image-14985 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/sparse-1.jpg\" alt=\"MongoDB Index- Sparse Indexes\" width=\"584\" height=\"176\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/sparse-1.jpg 584w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/sparse-1-150x45.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/sparse-1-300x90.jpg 300w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/a><p id=\"caption-attachment-14985\" class=\"wp-caption-text\">MongoDB Index- Sparse Indexes<\/p><\/div>\n<h3><span style=\"font-weight: 400;\">d.\u00a0<\/span>TTL Indexes<\/h3>\n<p><span style=\"font-weight: 400;\">TTL or \u201ctotal time to live\u201d indexes are the special indexes in MongoDB. These indexes are used to auto-delete documents from a collection after the specified time duration. The option that we use is expireAfterSecods to provide the expiration time. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">This property is ideal for certain types of information like machine-generated data, logs and session information that only need to be there for a finite amount of time in a database.<\/span><br \/>\n<span style=\"font-weight: 400;\">Let\u2019s create a TTL Index that deletes documents after 35 seconds.<\/span><\/p>\n<div id=\"attachment_14986\" style=\"width: 594px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/TTL.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-14986\" class=\"wp-image-14986 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/TTL.jpg\" alt=\"MongoDB Index- TTL Indexes\" width=\"584\" height=\"177\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/TTL.jpg 584w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/TTL-150x45.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/TTL-300x91.jpg 300w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/a><p id=\"caption-attachment-14986\" class=\"wp-caption-text\">MongoDB Index- TTL Indexes<\/p><\/div>\n<h2><strong>MongoDB Indexing Limitations<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Indexing in MongoDB has some limitations discussed below.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">a.\u00a0<\/span>Range Limitations<\/h3>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">A collection cannot have indexed more than 64.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The name of the index can contain only 164 characters.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">A compound index can have 31 fields indexed at max.<\/span><\/li>\n<\/ol>\n<h3>b. RAM Usage<\/h3>\n<p><span style=\"font-weight: 400;\">The total size of indexes must not exceed the RAM size as the indexes are stored in the RAM. If this limit exceeds, it will cause deletion of some indexes and deteriorate the performance.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">c.\u00a0<\/span>Query Limitations<\/h3>\n<p><span style=\"font-weight: 400;\">The following factors are there for queries while indexing.<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Queries should not use expressions like $nin, $not, etc.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Queries should not use arithmetic operators like $mod etc.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">A Query should not use $where clause.<\/span><\/li>\n<\/ol>\n<h3><span style=\"font-weight: 400;\">d.\u00a0<\/span>Indexed Key Limits<\/h3>\n<p><span style=\"font-weight: 400;\">MongoDB will not create an index if the value of existing index field exceeds the index key limit.<\/span><\/p>\n<h2>Conclusion<\/h2>\n<p><span style=\"font-weight: 400;\">Now we have learned Indexing in MongoDB. We have also seen the properties of Indexing and Indexing limitation in MongoDB. We will also see how replication and sharding work in MongoDB.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last MongoDB tutorial, we have discussed MongoDB Data Types.\u00a0So, let\u2019s move further with MongoDB. In this article, we will discuss MongoDB index, advanced indexing and indexing limitation in MongoDB. Moreover, we are&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":15832,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[2836,5070,5370,6682,6685,8827,8928,9421,12903,13177,14652,14976,15061,15150],"class_list":["post-14968","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","tag-compound-index","tag-geosapatial-index","tag-hashed-index","tag-index-properties","tag-indexing-limitations","tag-mongodb-indexing","tag-multikey-index","tag-partial-indexes","tag-single-field-index","tag-sparse-indexes","tag-text-index","tag-ttl-indexes","tag-types-of-index","tag-unique-indexes"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MongoDB Index: Types, Properties &amp; Limitations - DataFlair<\/title>\n<meta name=\"description\" content=\"MongoDB index,types of Index,Index Properties,Indexing Limitations,Single Field Index,Compound index,Multikey Index,Text Index,Hashed Index\" \/>\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-index\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB Index: Types, Properties &amp; Limitations - DataFlair\" \/>\n<meta property=\"og:description\" content=\"MongoDB index,types of Index,Index Properties,Indexing Limitations,Single Field Index,Compound index,Multikey Index,Text Index,Hashed Index\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/mongodb-index\/\" \/>\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=\"2018-05-14T03:48:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-09T07:37:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index.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=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MongoDB Index: Types, Properties &amp; Limitations - DataFlair","description":"MongoDB index,types of Index,Index Properties,Indexing Limitations,Single Field Index,Compound index,Multikey Index,Text Index,Hashed Index","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-index\/","og_locale":"en_US","og_type":"article","og_title":"MongoDB Index: Types, Properties &amp; Limitations - DataFlair","og_description":"MongoDB index,types of Index,Index Properties,Indexing Limitations,Single Field Index,Compound index,Multikey Index,Text Index,Hashed Index","og_url":"https:\/\/data-flair.training\/blogs\/mongodb-index\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-05-14T03:48:33+00:00","article_modified_time":"2021-05-09T07:37:28+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index.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":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/mongodb-index\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-index\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"MongoDB Index: Types, Properties &amp; Limitations","datePublished":"2018-05-14T03:48:33+00:00","dateModified":"2021-05-09T07:37:28+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-index\/"},"wordCount":1182,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-index\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index.jpg","keywords":["Compound index","geosapatial index","Hashed Index","Index Properties","Indexing Limitations","MongoDB indexing","Multikey Index","Partial indexes","Single Field Index","Sparse indexes","Text Index","TTL indexes","types of Index","Unique indexes"],"articleSection":["MongoDB Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/mongodb-index\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/mongodb-index\/","url":"https:\/\/data-flair.training\/blogs\/mongodb-index\/","name":"MongoDB Index: Types, Properties &amp; Limitations - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-index\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-index\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index.jpg","datePublished":"2018-05-14T03:48:33+00:00","dateModified":"2021-05-09T07:37:28+00:00","description":"MongoDB index,types of Index,Index Properties,Indexing Limitations,Single Field Index,Compound index,Multikey Index,Text Index,Hashed Index","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-index\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/mongodb-index\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/mongodb-index\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/MongoDB-Index.jpg","width":1200,"height":628,"caption":"MongoDB Index"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/mongodb-index\/#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 Index: Types, Properties &amp; Limitations"}]},{"@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\/14968","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=14968"}],"version-history":[{"count":7,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/14968\/revisions"}],"predecessor-version":[{"id":93190,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/14968\/revisions\/93190"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/15832"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=14968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=14968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=14968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}