A MongoDB text index gives you full-text search on string fields without standing up a separate search service. It tokenizes, stems, removes stop words, indexes the resulting terms, and exposes them through the $text operator with a relevance score. It is good enough for in-product search of moderate corpora and search-as-you-type on admin tools; for production-scale relevance ranking, faceting, fuzzy matching, and multi-language analysis, you graduate to Atlas Search or a dedicated engine.
How to verify
Create a text index, run a search, and inspect the score:
mongosh --host primary.example.internal:27017
use blog
db.posts.createIndex(
{ title: "text", body: "text" },
{ weights: { title: 10, body: 1 }, default_language: "english", name: "ix_posts_text" })
db.posts.find({ $text: { $search: "kubernetes \"rolling update\"" } }, { score: { $meta: "textScore" } })
.sort({ score: { $meta: "textScore" } }).limit(10)
db.posts.getIndexes()
Phrases in double quotes match as exact phrases; bare terms match any of them. Prefixing a term with - excludes documents containing it.
What’s happening
A text index is a special type of compound index where the indexed values are tokens extracted from the source string. For each indexed document, MongoDB runs the language-specific analyzer (lowercasing, stemming, stop-word removal), then writes one index entry per resulting token. The $text query operator parses the search string the same way and looks up matching tokens.
Each $text query returns a relevance score available via { $meta: "textScore" }. The score is a function of term frequency in the document, the index weight assigned to the field, and the inverse document frequency. You can project it to inspect, sort by it for relevance ordering, or both. Without sort({ score: { $meta: "textScore" } }), results come back in arbitrary order.
A collection can have at most one text index — but that index can cover multiple fields with different weights. Use this to bias title matches over body matches, or product name over description. Wildcard text indexes ({ "$**": "text" }) cover every string field in every document — useful for schemaless catalogues, expensive for hot write paths.
The procedure
-
Pick the fields and weights. Title fields should weigh 10× a body field. SKU codes and identifiers should weigh more than free text.
db.products.createIndex( { name: "text", description: "text", sku: "text", tags: "text" }, { weights: { name: 20, sku: 50, tags: 5, description: 1 } } ) -
Set the language.
default_languagecontrols stemming and stop words globally;language_overrideon a per-document field lets you store multi-language documents in one collection.db.posts.createIndex( { body: "text" }, { default_language: "english", language_override: "lang" } ) db.posts.insertOne({ body: "le chat noir", lang: "french" }) -
Query with the
$textoperator. Always project and sort by score for ranked results.db.products.find( { $text: { $search: "wireless headphones noise-cancelling" } }, { name: 1, score: { $meta: "textScore" } } ).sort({ score: { $meta: "textScore" } }).limit(20) -
Combine with other predicates. A
$textpredicate can coexist with other filters —tenantId,status, date ranges. The planner uses the text index to find candidates, then applies the other filters.db.products.find({ tenantId: 42, status: "active", $text: { $search: "wireless" } }) -
Use case-sensitive or diacritic-sensitive search when needed. Defaults are insensitive on both axes.
db.posts.find({ $text: { $search: "RUST", $caseSensitive: true } })
Operational notes
- One text index per collection. Adding a second fails. To change fields or weights, drop the existing one first.
- Stop words depend on the language. “The”, “a”, “an” disappear in English; “le”, “la”, “un” in French. A search for “the matrix” finds documents containing “matrix” — by design, not a bug.
$textdoes not support$orwith non-text predicates in the same way as other operators. Build the query around the text criterion as the primary filter.- Scoring is not relevance. A high
textScoremeans the document has many matching tokens with high weights, not that the user wanted it. For learned relevance ranking, use Atlas Search or /en/knowledge-base/elasticsearch-search-query-tuning/. - Text indexes are not multi-key compoundable with arbitrary other fields in a way that uses the index for both the equality and the text search efficiently. Compound text indexes exist (prefix equality + text) but have limitations on which queries can use them.
- Graduate when the search needs facets, autocomplete, typo-tolerance, or vector embeddings. Atlas Search (/en/knowledge-base/mongodb-atlas-search-integration/) covers the next tier without leaving the MongoDB ecosystem.
Stack Harbor configures text indexes for clients whose corpora fit in MongoDB and whose relevance requirements are modest — internal admin tools, catalogue prefix search, support ticket lookup. When the requirement grows, we move the workload to Atlas Search or a dedicated engine without changing the application data model. See /en/services/managed-operations/ for the engagement model.