How to Use Text Search in MongoDB: Syntax and Examples
To use text search in MongoDB, first create a
text index on the fields you want to search. Then, use the $text operator in your query to find documents matching the search terms.Syntax
Text search in MongoDB requires two main steps: creating a text index on the fields you want to search, and querying with the $text operator.
- Creating text index: Use
db.collection.createIndex({ field: "text" })to enable text search on a field. - Querying text: Use
{ $text: { $search: "search terms" } }to find documents matching the terms.
mongodb
db.collection.createIndex({ fieldName: "text" })
db.collection.find({ $text: { $search: "your search terms" } })Example
This example shows how to create a text index on the description field and search for documents containing the word "coffee".
mongodb
use testdb // Create collection and insert sample documents db.products.insertMany([ { name: "Coffee Maker", description: "Makes great coffee" }, { name: "Tea Kettle", description: "Boils water for tea" }, { name: "Espresso Machine", description: "Brews espresso coffee" } ]) // Create a text index on the description field db.products.createIndex({ description: "text" }) // Search for documents containing 'coffee' db.products.find({ $text: { $search: "coffee" } })
Output
[
{ "_id": ObjectId("..."), "name": "Coffee Maker", "description": "Makes great coffee" },
{ "_id": ObjectId("..."), "name": "Espresso Machine", "description": "Brews espresso coffee" }
]
Common Pitfalls
Common mistakes when using text search in MongoDB include:
- Not creating a text index before querying with
$text. Queries will fail or return no results. - Trying to create multiple text indexes on the same collection. MongoDB allows only one text index per collection.
- Expecting case-sensitive or partial word matches. Text search is case-insensitive but matches whole words or stems.
- Not using quotes for phrase search, which can lead to unexpected results.
mongodb
/* Wrong: Querying without text index */ db.products.find({ $text: { $search: "coffee" } }) /* Right: Create text index first */ db.products.createIndex({ description: "text" }) db.products.find({ $text: { $search: "coffee" } })
Quick Reference
| Action | Command Example |
|---|---|
| Create text index | db.collection.createIndex({ field: "text" }) |
| Search text | db.collection.find({ $text: { $search: "term" } }) |
| Search phrase | db.collection.find({ $text: { $search: '"exact phrase"' } }) |
| Exclude words | db.collection.find({ $text: { $search: "coffee -tea" } }) |
| Sort by relevance | db.collection.find({ $text: { $search: "term" } }, { score: { $meta: "textScore" } }).sort({ score: { $meta: "textScore" } }) |
Key Takeaways
Always create a text index on the fields before using $text search.
$text queries match whole words and are case-insensitive by default.
Only one text index is allowed per collection in MongoDB.
Use quotes in $search to find exact phrases.
Sort results by relevance using the textScore metadata.