Text Indexes
Text Indexes: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.
On this page
MongoDB Tutorial · Lesson 44 of 100
Text Indexes
Foundations & CRUD ✓ → Queries & Schema → Aggregation & Scale → Atlas & Projects
Queries & Schema · 2 — Design · ~6 min · MongoDB — Indexing & Performance
What is this?
A text index tokenizes string fields for word search using $text and $search scoring. It is language-aware and different from regex.
Why should you care?
Help centers and course catalogs need “search lessons about replica sets” without building Elasticsearch on day one.
See it live — copy this example
Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.
db.articles.insertMany([
{ title: "Replica set elections", body: "How primaries are chosen" },
{ title: "Sharding chunk migration", body: "Balancing data across shards" }
])
db.articles.createIndex({ title: "text", body: "text" })
db.articles.find({ $text: { $search: "replica elections" } }, { score: { $meta: "textScore" } })
.sort({ score: { $meta: "textScore" } })
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- One text index covers title and body.
- $text searches tokens.
- Projecting and sorting by textScore ranks better matches first.
- Only one text index per collection is allowed.
Practice next
- Insert articles and create the text index.
- Search for "sharding" and "elections".
- Try phrase search with \"replica set\" in $search.
- Add weights: { title: 10, body: 5 } on createIndex.
- Exclude stop words by relying on default language.
Remember
Text indexes enable $text search. Sort by textScore for relevance. One text index per collection.
In-app help search
Users type “failover” and articles rank by relevance.
Outcome: Support deflection improves without a separate search cluster yet.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!