Text Search — MongoDB Technical Interview Questions & Answers
Built-in $text search with a text index.
Text Search
- Simple meaning: Create a text index, then $text search for words in string fields.
- How to use: db.articles.createIndex({ title: "text", body: "text" }); db.articles.find({ $text: { $search: "mongodb index" } }).
- Atlas Search is richer on Atlas. $text is the built-in Community option.
- Common mistake: $regex everywhere on big text. Use a text index for word search.
- Interview tip: text index + $text. Mention language and stop words exist.
Technical Interview Tip: Text index, then $text search.
Code Examples for Text Search
Text Search — working example
db.articles.createIndex({ title: "text", body: "text" })
db.articles.find({ $text: { $search: "mongodb aggregation" } })Text search needs a text index; $text finds documents matching search terms.
How to Explain Text Search to an Interviewer
How does $text work?
Needs a text index find({ $text: { $search: "..." } })