Compound and Text Indexes
Part of the MERN Stack Roadmap
In short
An index on one field helps simple queries. Real screens filter by category, sort by price and search by keyword, and that is where compound and text indexes turn a slow page into an instant one.
A compound index covers several fields in a set order, such as category then price, and a text index supports keyword search across string fields.

Think of it like this
A phone directory and a book index
A phone directory is sorted by city, then by surname: perfect for "Sharmas in Jaipur" but useless for "everyone named Rahul". That's a compound index, where order matters. The index at the back of a textbook lists every important word with its pages. That's a text index.
Watch it happen
Index { category: 1, price: 1 } stores entries in this order
1/5Entries are sorted by category first, then price within each category.
The big ideas
What you need to know
Field order matters
A compound index helps queries on its prefix: { a, b, c } serves a, a+b and a+b+c, but not b alone.
The ESR guideline
Order fields as Equality first, then Sort, then Range. For example { category: 1, createdAt: -1, price: 1 }.
Text index
createIndex({ name: "text", description: "text" }) enables $text search with word stemming. One text index per collection.
Indexes have a cost
Each index uses memory and slows writes a little. Create them for real queries, not every field.
See it in code
Walk through the code
1// Compound: filter by category, sort by newest2db.products.createIndex({ category: 1, createdAt: -1 });3db.products.find({ category: "kitchen" }).sort({ createdAt: -1 }).limit(20);4 5// Text index across two fields, name weighted higher6db.products.createIndex(7 { name: "text", description: "text" },8 { weights: { name: 5, description: 1 }, name: "product_text" }9);10 11db.products12 .find({ $text: { $search: "steel bottle" } }, { score: { $meta: "textScore" }, name: 1 })13 .sort({ score: { $meta: "textScore" } })14 .limit(10);15 16db.products.getIndexes();1/4The index matches both the filter and the sort, so no in-memory sort is needed.
One compound index for the listing page, one text index for the search bar.
Save yourself the pain
Common mistakes
Oops
Creating separate single-field indexes on category and price and expecting them to act like one compound index.
Do this instead
Create one compound index in the order your queries use: equality fields first, then sort, then range.
Oops
Expecting $text to match partial words like "bott".
Do this instead
Text indexes match whole words and their stems. For typo-tolerant or as-you-type search, use Atlas Search.
Your mission
Index a product catalogue
Quick check
Which query can use the index { city: 1, age: 1 } efficiently?
Go deeper
- 1Create a compound index for a query that filters by category and sorts by price.
- 2Learn why the field order in a compound index matters.
- 3Add a text index on product name and description and search with $text.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.