$text is a MongoDB query operator for full-text search on fields covered by a text index. It filters documents that match your search terms—it does not transform field values like math operators do.
Remember
$text = search bar for indexed strings
{ $text: { $search: <string> } }
requires → text index on searched fields
terms → OR by default
phrase → "\"exact words\""
exclude → -word
one $text per query
$match + $text → must be first pipeline stage
Index first, then search. Optionally project and sort by relevance.
Create a text index on the string fields you want to search.
Run find() (or a first-stage $match) with { $text: { $search: "..." } }.
Optional: project { score: { $meta: "textScore" } } and sort by that score.
Optional: use phrases ("\"…\"") or exclusions (-word) in $search.
Piece
Required?
Role
Text index
Yes
Makes fields searchable with $text
$search
Yes
Search string (terms, phrase, exclusions)
$language / case / diacritic flags
No
Tune stemming and matching rules
$meta: "textScore"
No
Relevance for project / sort
Pseudocode
Pseudocode
create text index on title, body
for each document:
if no text index:
error
tokens = tokenize($search) // stop words dropped
if document matches any required terms
and none of the excluded terms:
keep document
optionally attach textScore
else:
skip
Reference
Cheat Sheet
Keep these facts handy while writing text-search queries.
Topic
Detail
Type
Query operator (filter), not a field expression
Syntax
{ $text: { $search: "terms" } }
Index
createIndex({ title: "text", body: "text" })
Default match
Terms are OR’d; stop words ignored
Phrase
$search: "\"exact phrase\""
Exclude
$search: "mongodb -sql"
Relevance
{ score: { $meta: "textScore" } }
Try it
Live Preview
Pick a search string and see which sample articles would match (simple term OR demo—same idea as $text + a text index).
Simulates term matching on three sample articles (not a real MongoDB server).
[
{
_id: 1,
title: "MongoDB Text Search",
body: "Learn how to search documents with a text index."
},
{
_id: 3,
title: "Aggregation Pipeline",
body: "MongoDB pipelines process documents step by step."
}
]
How It Works
Use the text index. MongoDB looks up the term mongodb in the index from Example 1.
Match either field. Doc 1 matches on title; doc 3 matches on body.
Skip non-matches. Doc 2 has no mongodb token, so it is left out.
Case is flexible by default.MongoDB in the data still matches search term mongodb.
Example 3 — Sort by textScore
Same search, but project the relevance score and sort best matches first.
[
{
_id: 1,
title: "MongoDB Text Search",
body: "Learn how to search documents with a text index.",
score: 1.1
},
{
_id: 3,
title: "Aggregation Pipeline",
body: "MongoDB pipelines process documents step by step.",
score: 0.75
}
]
Sort by that score. Higher scores mean a stronger match for the query.
Scores are relative. Exact numbers can differ by MongoDB version—use them for ordering, not absolute grades.
Edge Cases & Pitfalls
Check these before using $text in production queries.
index
Text index is required
Without a text index, $text fails. Create the index before you search.
once
Only one $text per query
You cannot combine two $text expressions. In aggregation, put $text in the first $match.
vs regex
Not a regex substitute for every case
Need partial character patterns? Use $regex. Need word search + scores? Use $text.
atlas
Atlas Search is a different feature
The Atlas $search stage is richer and separate from the self-managed $text query operator shown here.
Analysis
Performance Notes
Approach
Cost
Best for
$text + text index
Index lookup
Word / phrase search on content
$text + textScore sort
Higher (score work)
Ranked search results
$regex on large text
Often slower
Small sets or pattern needs
Index only the fields you search. Case- or diacritic-sensitive flags can slow queries. Combine other filters carefully—special indexes like geo do not mix with $text.
Remember
Key Takeaways
Role: full-text search filter on a text index.
Setup: create the text index before you query.
Syntax:{ $text: { $search: "terms" } }.
Ranking: sort with $meta: "textScore".
One line:$text answers “which documents match these words?”—after you add a text index.
Frequently Asked Questions
$text performs full-text search on string fields covered by a text index. Use it in find() filters and in a $match stage (must be the first stage) in aggregation.
Yes. Create a text index on the fields you want to search. Without a text index, MongoDB returns an error when you run a $text query.
Use { $text: { $search: "terms" } }. Optional fields include $language, $caseSensitive, and $diacriticSensitive.
$text uses a text index for word/phrase search and relevance scoring. $regex matches patterns and is better for small pattern filters. For searchable content at scale, prefer $text.
Project or sort with { $meta: "textScore" }. In find(), add score to the projection and sort by that score descending.
No. A query can include only one $text expression. In aggregation, the $match that uses $text must be the first stage.