Skip to content
Learn Netverks

Lesson

Step 15/36 42% through track

compound-text-indexes

Compound and text indexes

Last reviewed May 28, 2026 Content v20260528
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Compound and text indexes: document modeling, query operators, and aggregation patterns for MongoDB.

COLLSCAN on hot APIs is a production incident—compound indexes must match filter + sort shape.

You will apply Compound and text indexes in contexts like: Slow API list endpoints, admin search, and ops firefighting on COLLSCAN alerts.

Copy JavaScript shell queries from each lesson into mongosh or MongoDB Atlas Data Explorer—inspect matched documents and explain plans. The in-browser lab (execution_profile: none) ships later; mongosh is the practice path now. Also check explain executionStats for IXSCAN vs COLLSCAN.

When you can explain the previous lesson's ideas without copying example queries verbatim.

Compound indexes support multi-field filters and sorts. Text indexes power basic full-text search on string fields.

Compound index for filter + sort

db.products.createIndex({ category: 1, price: -1 })
db.products.find({ category: 'tools' }).sort({ price: -1 }).limit(5)

Practice: Run on practice in mongosh.

Text index

db.articles.createIndex({ title: 'text', body: 'text' })
db.articles.find({ $text: { $search: 'mongodb index' } }, { score: { $meta: 'textScore' } })
  .sort({ score: { $meta: 'textScore' } })

Index limits

One text index per collection (combined fields). For advanced search, consider Atlas Search or external engines.

Important interview questions and answers

  1. Q: Prefix rule?
    A: Compound index {a:1,b:1} helps {a} and {a,b} queries—not always {b} alone.
  2. Q: text index language?
    A: Default language stemming affects tokenization.

Self-check

  1. Why sort price -1 in compound index?
  2. What operator uses text index?

Tip: Compound index field order must match equality-then-sort query shape.

Interview prep

Compound prefix?
Index {a:1,b:1} supports {a} and {a,b} queries.
$text search?
Uses text index on string fields.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Compound order?
  • $text search?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump