Chapter 22: Indexing & Search

1. What is Indexing in MongoDB? (The Big Picture)

Imagine your collection is a huge library with 1 million books (documents).

Without indexing:

  • To find all books written by “Rahul Sharma” → librarian has to check every single book one by one → COLLSCAN (Collection Scan) → very slow!

With indexing:

  • The librarian has a sorted phonebook-style list (index) of authors → jumps directly to “Rahul Sharma” section → finds only those books → IXSCAN (Index Scan) → lightning fast!

Official simple definition (2026 style):

An index is a special data structure (usually B-tree) that stores a small portion of the collection’s data in an easy-to-traverse, sorted form. It allows MongoDB to avoid scanning every document for most queries.

Without indexes → queries scan entire collection → performance dies as data grows. With good indexes → queries return in milliseconds even on billions of documents.

2. Why Indexing Matters in 2026 (Real-World Reasons)

Reason Without Index (COLLSCAN) With Proper Index (IXSCAN) Typical speedup
1 million documents 500–5000 ms <10 ms 50–500×
Filter + sort on common fields Full scan + in-memory sort Uses index for both filter + sort 100–1000×
Text / partial search Full scan (very slow) Text index or Atlas Search 100–10,000×
Sharded cluster balance Hashed indexes for even distribution
High write throughput Too many indexes → slower writes Balance needed

3. Main Types of Indexes in MongoDB (with Examples)

MongoDB supports many index types — here are the ones you’ll use 95% of the time:

A. Single Field Index (Most Basic & Common)

JavaScript

→ Great for: equality (email: “rahul@hyd.in”), range (age: {$gt: 18}), sort.

B. Compound Index (Multiple Fields – Very Powerful)

Order matters — follows Equality → Sort → Range (ESR) rule.

JavaScript

→ Prefixes: MongoDB can use left-to-right parts of compound index.

C. Text Index (For Keyword / Full-Text Search – Legacy Style)

Only one text index per collection.

JavaScript

→ Gives relevance score via { $meta: “textScore” }

But in 2026, most people prefer Atlas Search instead (more powerful — see below).

D. Hashed Index (Mostly for Sharding)

JavaScript

→ Distributes data evenly across shards — not for normal queries.

E. Other Common Types (Quick Overview)

  • TTL Index — auto-delete old documents db.logs.createIndex({ expireAt: 1 }, { expireAfterSeconds: 0 })
  • Geospatial — 2dsphere for location queries db.places.createIndex({ location: “2dsphere” })
  • Multikey — automatic when indexing arrays

4. Atlas Search – The Modern Full-Text & Vector Search (Very Hot in 2026)

Regular indexes (B-tree) → great for exact matches, ranges, sorts Atlas Search → built on Apache Lucene (inverted index) → full-text, fuzzy, autocomplete, synonyms, vector search for AI embeddings

Key differences (2026):

Feature Regular Index (B-tree) Atlas Search (Lucene-based)
Use case Exact equality, range, sort Full-text, partial match, relevance, vectors
Syntax find({ name: “Rahul” }) $search operator in aggregation
Speed on text search Slow (regex / $text limited) Very fast
Relevance scoring No Yes (BM25, custom)
Fuzzy / autocomplete No Yes
Vector / AI similarity No Yes (very hot in 2026)
Where available All MongoDB Only MongoDB Atlas

Quick Atlas Search example (in Atlas UI → create Search Index first):

JavaScript

5. How to Create, View & Manage Indexes (Practical Commands)

JavaScript

Look for:

  • stage: “COLLSCAN” → bad (full scan)
  • stage: “IXSCAN” → good (using index)

6. Quick Summary Table – When to Use Which

Your Query Pattern Recommended Index Type Example Command Snippet
Exact match on one field Single field { email: 1 }
Filter + sort on 2–3 fields Compound (ESR order) { status: 1, createdAt: -1 }
Keyword / full-text search (legacy) Text index { title: “text”, body: “text” }
Full-text + fuzzy + relevance + vectors Atlas Search index Create in Atlas UI → use $search
Shard key distribution Hashed { userId: “hashed” }
Auto-delete old logs TTL { expireAt: 1 }, { expireAfterSeconds: 0 }

7. Mini Exercise – Try Right Now!

  1. Create compound index on { city: 1, age: -1 }
  2. Run find({ city: “Hyderabad” }).sort({ age: -1 }) → check .explain()
  3. Create text index on articles collection → try $text search
  4. (If on Atlas) Create Atlas Search index → try $search for “mongodb tutorial”

Understood beta? Indexing is the #1 performance lever in MongoDB — do it right and your app flies; ignore it and even simple queries crawl.

Next class — what do you want?

  • Deep dive into Atlas Search + vector search (very hot in 2026 for AI apps)?
  • Index intersection vs compound — when which wins?
  • explain() deep dive + slow query log?
  • Or performance tuning mini-project with real data?

Tell me — class is yours! 🚀❤️

Any doubt so far? Ask anything — no question is silly 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *