|
| 1 | +--- |
| 2 | +id: mongodb-advanced-indexing |
| 3 | +title: MongoDB - Advanced Indexing |
| 4 | +sidebar_label: Advanced Indexing |
| 5 | +sidebar_position: 6 |
| 6 | +tags: [mongodb, indexing, array indexing, sub-document indexing] |
| 7 | +description: Learn how to create advanced indexes in MongoDB, including indexing array fields and sub-document fields for optimized query performance. |
| 8 | +--- |
| 9 | + |
| 10 | +## Indexing Array Fields |
| 11 | + |
| 12 | +We have inserted the following document in the collection named `users`: |
| 13 | + |
| 14 | +```javascript |
| 15 | +db.users.insert( |
| 16 | + { |
| 17 | + "address": { |
| 18 | + "city": "Los Angeles", |
| 19 | + "state": "California", |
| 20 | + "pincode": "123" |
| 21 | + }, |
| 22 | + "tags": [ |
| 23 | + "music", |
| 24 | + "cricket", |
| 25 | + "blogs" |
| 26 | + ], |
| 27 | + "name": "Tom Benzamin" |
| 28 | + } |
| 29 | +) |
| 30 | +``` |
| 31 | + |
| 32 | +The above document contains an `address` sub-document and a `tags` array. |
| 33 | + |
| 34 | +### Creating an Index on Array Fields |
| 35 | + |
| 36 | +Suppose we want to search user documents based on the user’s tags. For this, we will create an index on the `tags` array in the collection. |
| 37 | + |
| 38 | +Creating an index on an array in turn creates separate index entries for each of its fields. So in our case, when we create an index on the `tags` array, separate indexes will be created for its values `music`, `cricket`, and `blogs`. |
| 39 | + |
| 40 | +To create an index on the `tags` array, use the following code: |
| 41 | + |
| 42 | +```javascript |
| 43 | +db.users.createIndex({"tags":1}) |
| 44 | +``` |
| 45 | + |
| 46 | +```json |
| 47 | +{ |
| 48 | + "createdCollectionAutomatically" : false, |
| 49 | + "numIndexesBefore" : 2, |
| 50 | + "numIndexesAfter" : 3, |
| 51 | + "ok" : 1 |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +After creating the index, we can search on the `tags` field of the collection like this: |
| 56 | + |
| 57 | +```javascript |
| 58 | +db.users.find({tags:"cricket"}).pretty() |
| 59 | +``` |
| 60 | + |
| 61 | +```json |
| 62 | +{ |
| 63 | + "_id" : ObjectId("5dd7c927f1dd4583e7103fdf"), |
| 64 | + "address" : { |
| 65 | + "city" : "Los Angeles", |
| 66 | + "state" : "California", |
| 67 | + "pincode" : "123" |
| 68 | + }, |
| 69 | + "tags" : [ |
| 70 | + "music", |
| 71 | + "cricket", |
| 72 | + "blogs" |
| 73 | + ], |
| 74 | + "name" : "Tom Benzamin" |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +To verify that proper indexing is used, use the following `explain` command: |
| 79 | + |
| 80 | +```javascript |
| 81 | +db.users.find({tags:"cricket"}).explain() |
| 82 | +``` |
| 83 | + |
| 84 | +This gives you the following result: |
| 85 | + |
| 86 | +```json |
| 87 | +{ |
| 88 | + "queryPlanner" : { |
| 89 | + "plannerVersion" : 1, |
| 90 | + "namespace" : "mydb.users", |
| 91 | + "indexFilterSet" : false, |
| 92 | + "parsedQuery" : { |
| 93 | + "tags" : { |
| 94 | + "$eq" : "cricket" |
| 95 | + } |
| 96 | + }, |
| 97 | + "queryHash" : "9D3B61A7", |
| 98 | + "planCacheKey" : "04C9997B", |
| 99 | + "winningPlan" : { |
| 100 | + "stage" : "FETCH", |
| 101 | + "inputStage" : { |
| 102 | + "stage" : "IXSCAN", |
| 103 | + "keyPattern" : { |
| 104 | + "tags" : 1 |
| 105 | + }, |
| 106 | + "indexName" : "tags_1", |
| 107 | + "isMultiKey" : false, |
| 108 | + "multiKeyPaths" : { |
| 109 | + "tags" : [ ] |
| 110 | + }, |
| 111 | + "isUnique" : false, |
| 112 | + "isSparse" : false, |
| 113 | + "isPartial" : false, |
| 114 | + "indexVersion" : 2, |
| 115 | + "direction" : "forward", |
| 116 | + "indexBounds" : { |
| 117 | + "tags" : [ |
| 118 | + "[\"cricket\", \"cricket\"]" |
| 119 | + ] |
| 120 | + } |
| 121 | + } |
| 122 | + }, |
| 123 | + "rejectedPlans" : [ ] |
| 124 | + }, |
| 125 | + "serverInfo" : { |
| 126 | + "host" : "Krishna", |
| 127 | + "port" : 27017, |
| 128 | + "version" : "4.2.1", |
| 129 | + "gitVersion" : "edf6d45851c0b9ee15548f0f847df141764a317e" |
| 130 | + }, |
| 131 | + "ok" : 1 |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +The above command resulted in `"stage" : "IXSCAN", "indexName" : "tags_1"` which confirms that proper indexing is used. |
| 136 | + |
| 137 | +## Indexing Sub-Document Fields |
| 138 | + |
| 139 | +Suppose that we want to search documents based on `city`, `state`, and `pincode` fields. Since all these fields are part of the `address` sub-document field, we will create an index on all the fields of the sub-document. |
| 140 | + |
| 141 | +For creating an index on all the three fields of the sub-document, use the following code: |
| 142 | + |
| 143 | +```javascript |
| 144 | +db.users.createIndex({"address.city":1,"address.state":1,"address.pincode":1}) |
| 145 | +``` |
| 146 | + |
| 147 | +```json |
| 148 | +{ |
| 149 | + "numIndexesBefore" : 4, |
| 150 | + "numIndexesAfter" : 4, |
| 151 | + "note" : "all indexes already exist", |
| 152 | + "ok" : 1 |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +Once the index is created, we can search for any of the sub-document fields utilizing this index as follows: |
| 157 | + |
| 158 | +```javascript |
| 159 | +db.users.find({"address.city":"Los Angeles"}).pretty() |
| 160 | +``` |
| 161 | + |
| 162 | +```json |
| 163 | +{ |
| 164 | + "_id" : ObjectId("5dd7c927f1dd4583e7103fdf"), |
| 165 | + "address" : { |
| 166 | + "city" : "Los Angeles", |
| 167 | + "state" : "California", |
| 168 | + "pincode" : "123" |
| 169 | + }, |
| 170 | + "tags" : [ |
| 171 | + "music", |
| 172 | + "cricket", |
| 173 | + "blogs" |
| 174 | + ], |
| 175 | + "name" : "Tom Benzamin" |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +Remember that the query expression has to follow the order of the index specified. So the index created above would support the following queries: |
| 180 | + |
| 181 | +```javascript |
| 182 | +db.users.find({"address.city":"Los Angeles","address.state":"California"}).pretty() |
| 183 | +``` |
| 184 | + |
| 185 | +```json |
| 186 | +{ |
| 187 | + "_id" : ObjectId("5dd7c927f1dd4583e7103fdf"), |
| 188 | + "address" : { |
| 189 | + "city" : "Los Angeles", |
| 190 | + "state" : "California", |
| 191 | + "pincode" : "123" |
| 192 | + }, |
| 193 | + "tags" : [ |
| 194 | + "music", |
| 195 | + "cricket", |
| 196 | + "blogs" |
| 197 | + ], |
| 198 | + "name" : "Tom Benzamin" |
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +### Diagram (Mermaid) |
| 203 | + |
| 204 | +Here is a visual representation of the document structure and indexing process: |
| 205 | + |
| 206 | +```mermaid |
| 207 | +graph TD; |
| 208 | + A[Document: users] |
| 209 | + A --> B[Sub-document: address] |
| 210 | + A --> C[Array: tags] |
| 211 | + B --> D[city] |
| 212 | + B --> E[state] |
| 213 | + B --> F[pincode] |
| 214 | + C --> G[music] |
| 215 | + C --> H[cricket] |
| 216 | + C --> I[blogs] |
| 217 | +``` |
| 218 | + |
| 219 | +### Notes |
| 220 | + |
| 221 | +- Indexing array fields in MongoDB creates separate index entries for each value in the array. |
| 222 | +- Indexing sub-document fields allows efficient queries on nested fields. |
| 223 | +- Ensure the query order matches the index order to fully utilize the indexes. |
| 224 | + |
| 225 | +### Table |
| 226 | + |
| 227 | +| Field | Type | Indexed | Description | |
| 228 | +|-----------------|------------------|---------|------------------------------------------| |
| 229 | +| `address` | Sub-document | Yes | Contains nested fields for address info. | |
| 230 | +| `address.city` | String | Yes | City name in the address. | |
| 231 | +| `address.state` | String | Yes | State name in the address. | |
| 232 | +| `address.pincode`| String | Yes | Pincode in the address. | |
| 233 | +| `tags` | Array of Strings | Yes | User's tags for indexing. | |
0 commit comments