Table of Contents
- Introduction
- What is Full-Text Search?
- Why Use Text Indexes in MongoDB
- Creating Text Indexes
- Performing Text Search
- Text Index Rules and Limitations
- Filtering, Sorting, and Scoring
- Multi-language Support in Text Indexes
- Text Indexes vs Regex for Search
- Real-World Use Cases
- Best Practices for Text Search
- Conclusion
1. Introduction
Text search is a critical capability for modern applications — whether you’re building a blog, an e-commerce site, or a document management system. MongoDB simplifies full-text search with text indexes, which allow efficient querying of string content stored in your documents.
In this module, you’ll learn everything from how to create and use text indexes, to advanced search techniques, language support, and how MongoDB scores and filters results.
2. What is Full-Text Search?
Full-text search enables querying human-readable text in a more natural way, accounting for linguistic nuances such as stemming, stop words, and relevance scoring. Unlike regular expression matching, full-text search is token-based and optimized for performance and ranking.
MongoDB supports this feature through text indexes on string fields.
3. Why Use Text Indexes in MongoDB
- Efficient search: Text indexes tokenize and index the document content for fast lookup.
- Natural language processing: Built-in support for stop words and stemming.
- Relevance-based ranking: Results can be scored and sorted based on match quality.
- Multi-language support: Different linguistic rules for over 30 languages.
4. Creating Text Indexes
You can create a text index on a single field or multiple fields:
jsCopyEdit// Create a text index on a single field
db.articles.createIndex({ content: "text" });
// Create a text index on multiple fields
db.articles.createIndex({ title: "text", content: "text" });
MongoDB only allows one text index per collection, but that index can cover multiple fields.
5. Performing Text Search
Use the $text
operator in the find()
query to search indexed fields:
jsCopyEditdb.articles.find({ $text: { $search: "mongodb indexing" } });
This query matches any article where the title or content contains “mongodb” or “indexing”.
Exact Phrases
To search for an exact phrase, enclose it in quotes:
jsCopyEditdb.articles.find({ $text: { $search: "\"mongodb indexing\"" } });
Excluding Terms
Use -
to exclude terms:
jsCopyEditdb.articles.find({ $text: { $search: "mongodb -replica" } });
6. Text Index Rules and Limitations
- One text index per collection.
- Only string fields are supported.
- Text indexes are case-insensitive and diacritic-insensitive.
- Fields not explicitly indexed with
"text"
will be ignored by$text
queries.
7. Filtering, Sorting, and Scoring
MongoDB assigns a relevance score to each matching document. You can access it using the textScore
metadata:
jsCopyEditdb.articles.find(
{ $text: { $search: "mongodb performance" } },
{ score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } });
This ensures the results are sorted by how relevant they are to the search terms.
You can also combine $text
with other query operators:
jsCopyEditdb.articles.find({
$text: { $search: "caching" },
views: { $gt: 1000 }
});
8. Multi-language Support in Text Indexes
Text indexes support multiple languages by applying different linguistic rules such as stemming and stop words.
When creating the index, specify a default language:
jsCopyEditdb.articles.createIndex(
{ title: "text", content: "text" },
{ default_language: "french" }
);
You can also override this per-document using the language
field:
jsCopyEditdb.articles.insert({
title: "MongoDB en action",
content: "Apprenez MongoDB avec des exemples",
language: "french"
});
9. Text Indexes vs Regex for Search
Feature | Text Indexes | Regex |
---|---|---|
Speed | Fast (indexed) | Slow (no index support) |
Case-insensitive | Yes | Needs /i modifier |
Stemming | Yes | No |
Diacritic sensitivity | No | Yes |
Scoring/Relevance | Yes | No |
Conclusion: Use text indexes for natural language queries and regex for pattern matching.
10. Real-World Use Cases
- Blog/Search Engine: Index titles and content for quick searching.
- E-commerce: Product names and descriptions.
- Support Portals: Searching FAQ or documentation.
- Messaging apps: Keyword search in chat histories.
- CMS: Finding articles by keywords or phrases.
11. Best Practices for Text Search
- Use compound indexes when combining
$text
with filters (e.g.,category
). - Avoid over-indexing: Only include relevant fields in the text index.
- Use
$text
sparingly on large collections — it’s powerful but can be CPU intensive. - Cache frequent searches at the application level.
- Consider Atlas Search (based on Lucene) for advanced capabilities.
12. Conclusion
Text indexes in MongoDB provide a powerful, flexible, and efficient way to implement full-text search. With the $text
operator, scoring, language support, and stemming, you can create search features that are responsive and user-friendly.