← MongoDB learning path
MongoDB · Module 5 of 10

Indexes

Speed up queries with indexes.

📖 Key ideas

  1. Without indexes, MongoDB scans every document (COLLSCAN).
  2. createIndex({ email: 1 }) speeds lookups by email.
  3. Unique indexes prevent duplicates: { unique: true }.
  4. Compound indexes cover multi-field queries.
  5. explain() shows if an index is used.
💡 Real-life example: An index is the index at the back of a textbook: jump straight to the page.
IndexStructure for fast lookups
COLLSCANFull collection scan
Compound indexIndex on several fields
explain()Shows query plan