Day 4 of 5
⏱ ~60 minutes
MongoDB in 5 Days — Day 4

Indexes and Performance

Create single, compound, and text indexes. Use explain() to analyze query plans.

Why Indexes Matter

Without an index, MongoDB scans every document to find matches. With an index, it jumps directly to matching documents. On a million-document collection, the difference is seconds vs milliseconds.

Creating Indexes
// Single-field index
db.users.createIndex({ email: 1 })

// Unique index
db.users.createIndex({ email: 1 }, { unique: true })

// Compound index
db.products.createIndex({ category: 1, price: -1 })

// Text index for full-text search
db.products.createIndex({ name: 'text', description: 'text' })

// Sparse index (only index documents that have the field)
db.users.createIndex({ phone: 1 }, { sparse: true })

// List indexes
db.users.getIndexes()

// Drop an index
db.users.dropIndex('email_1')
explain(): Understand Query Plans
// Without explain
db.users.find({ email: '[email protected]' })

// With explain
db.users.find({ email: '[email protected]' }).explain('executionStats')

// Key fields to check:
// executionStats.nReturned → documents returned
// executionStats.totalDocsExamined → documents scanned
// If nReturned << totalDocsExamined → you need an index
// executionStats.executionTimeMillis → time in ms
💡
Index selectivity matters. An index on status where 90% of documents have status='active' is nearly useless — it still scans most documents. Index fields with high cardinality (many unique values): email, username, ID.
📝 Day 4 Exercise
Analyze Query Performance
  1. R
  2. u
  3. n
  4. <
  5. c
  6. o
  7. d
  8. e
  9. >
  10. e
  11. x
  12. p
  13. l
  14. a
  15. i
  16. n
  17. (
  18. )
  19. <
  20. /
  21. c
  22. o
  23. d
  24. e
  25. >
  26. o
  27. n
  28. y
  29. o
  30. u
  31. r
  32. m
  33. o
  34. s
  35. t
  36. c
  37. o
  38. m
  39. m
  40. o
  41. n
  42. q
  43. u
  44. e
  45. r
  46. y
  47. .
  48. N
  49. o
  50. t
  51. e
  52. t
  53. o
  54. t
  55. a
  56. l
  57. D
  58. o
  59. c
  60. s
  61. E
  62. x
  63. a
  64. m
  65. i
  66. n
  67. e
  68. d
  69. .
  70. C
  71. r
  72. e
  73. a
  74. t
  75. e
  76. a
  77. n
  78. i
  79. n
  80. d
  81. e
  82. x
  83. .
  84. R
  85. u
  86. n
  87. e
  88. x
  89. p
  90. l
  91. a
  92. i
  93. n
  94. (
  95. )
  96. a
  97. g
  98. a
  99. i
  100. n
  101. .
  102. C
  103. o
  104. m
  105. p
  106. a
  107. r
  108. e
  109. t
  110. h
  111. e
  112. e
  113. x
  114. e
  115. c
  116. u
  117. t
  118. i
  119. o
  120. n
  121. t
  122. i
  123. m
  124. e
  125. s
  126. a
  127. n
  128. d
  129. d
  130. o
  131. c
  132. s
  133. e
  134. x
  135. a
  136. m
  137. i
  138. n
  139. e
  140. d
  141. .

Day 4 Summary

  • Indexes trade write speed for read speed. Index fields you query and sort by frequently.
  • Compound indexes: order matters. {category:1, price:-1} supports queries on category alone or category+price.
  • explain('executionStats') shows docs scanned vs returned. High ratio = missing index.
  • Text indexes enable $text: { $search: 'keyword' } full-text search.
Finished this lesson?