Step 0 — Create a Text Index
Before using textScore, create a text index on the fields you want to search:
db.articles.createIndex({
title: "text",
body: "text"
}) 
The $meta operator returns metadata about a document in a projection or sort. The most common use is textScore — the relevance score from a $text full-text search query.
Extra query info.
Search relevance.
Full-text search.
Include score field.
Best matches first.
Prerequisite setup.
In MongoDB, the $meta operator exposes metadata that the server computes during a query. For full-text search, when you use $text: { $search: "..." }, MongoDB ranks matching documents. { $meta: "textScore" } lets you read that relevance score in the result set and sort by it.
Higher textScore values generally mean a better match. This is useful for search UIs, article listings, and any feature where users expect the most relevant results first.
$meta is a projection and sort operator, not a general math expression like $add. You use it in the projection document of find(), inside $project / $addFields, and in $sort stages.
Use $meta in projections and sorts with the metadata keyword "textScore":
find() or $project){ <fieldName>: { $meta: "textScore" } } .sort() or $sort){ <fieldName>: { $meta: "textScore" } } // 1. Create a text index
db.articles.createIndex({
title: "text",
body: "text"
})
// 2. Query with $text
{ $text: { $search: "mongodb tutorial" } }
// 3. Project the score
{ score: { $meta: "textScore" } } textScore — the primary metadata type for full-text search relevance.$text — the query must include a $text operator.score or relevance).$sort to order by relevance (typically -1 for best first).$meta: "textScore" returns a score only when the query includes $text. Without it, the metadata field will not contain a meaningful relevance value. Atlas Search and other search products use different APIs.
| Question | Answer |
|---|---|
| Operator type | Projection / sort metadata operator |
| Common metadata | "textScore" |
| Projection syntax | { score: { $meta: "textScore" } } |
| Requires | Text index + $text in query |
| Common stages | $match + $project + $sort |
{
title: 1,
score: {
$meta: "textScore"
}
}Add relevance field
{
$sort: {
score: {
$meta: "textScore"
}
}
}Sort by relevance
db.articles.find(
{ $text: {
$search: "mongo"
}},
{ score: {
$meta: "textScore"
}}
)Works in find too
{
$match: {
$text: {
$search: "query"
}
}
}Text search filter
Walk through an articles collection with a text index, run a $text search, and retrieve relevance scores with $meta.
Set up full-text search on an articles collection and rank results by relevance.
Before using textScore, create a text index on the fields you want to search:
db.articles.createIndex({
title: "text",
body: "text"
}) Suppose you have these documents in articles:
[
{
"_id": ObjectId("609c26812e9274a86871bc6a"),
"title": "MongoDB Aggregation Tutorial",
"body": "Learn pipelines, $match, and $project stages."
},
{
"_id": ObjectId("609c26812e9274a86871bc6b"),
"title": "Introduction to Databases",
"body": "A beginner guide covering SQL and NoSQL options."
},
{
"_id": ObjectId("609c26812e9274a86871bc6c"),
"title": "Advanced MongoDB Tips",
"body": "Indexing, aggregation, and performance tuning for MongoDB."
}
] Search for “mongodb aggregation”, project the text score, and sort by relevance:
db.articles.aggregate([
{
$match: {
$text: { $search: "mongodb aggregation" }
}
},
{
$project: {
title: 1,
body: 1,
score: { $meta: "textScore" }
}
},
{
$sort: { score: { $meta: "textScore" } }
}
]) $match with $text filters to documents containing the search terms.$project adds a score field via { $meta: "textScore" }.$sort orders results so the best matches appear first.Use $meta outside aggregation and combine scores with other logic.
The same pattern works in a standard find() query:
db.articles.find(
{ $text: { $search: "mongodb" } },
{
title: 1,
score: { $meta: "textScore" }
}
).sort({ score: { $meta: "textScore" } }) The second argument to find() is the projection. Include { score: { $meta: "textScore" } } to add the relevance field, then chain .sort() with the same meta reference.
Add a relevance field without removing other fields:
db.articles.aggregate([
{
$match: {
$text: { $search: "tutorial" }
}
},
{
$addFields: {
relevance: { $meta: "textScore" }
}
},
{
$sort: { relevance: -1 }
}
]) $addFields keeps all existing fields and adds relevance. After projecting with $meta, you can sort by the field name directly (relevance: -1) in later stages.
Keep only results above a score threshold using $match after projecting:
db.articles.aggregate([
{
$match: {
$text: { $search: "mongodb" }
}
},
{
$addFields: {
score: { $meta: "textScore" }
}
},
{
$match: {
score: { $gte: 1.0 }
}
},
{
$sort: { score: -1 }
}
]) Project textScore first, then filter on the numeric score field. This helps remove weak matches from search results.
Project the score first, then categorize match quality in a second stage:
db.articles.aggregate([
{
$match: {
$text: { $search: "mongodb" }
}
},
{
$addFields: {
score: { $meta: "textScore" }
}
},
{
$addFields: {
matchQuality: {
$cond: [
{ $gte: [ "$score", 2 ] },
"strong",
"weak"
]
}
}
}
]) Add textScore with $meta first, then use the numeric score field in $cond. Threshold values depend on your data and index — tune them with sample queries.
textScore during development to tune queries and indexes.$meta WorksMongoDB runs a full-text search and computes a relevance score per matching document.
In $project or find projection, { $meta: "textScore" } exposes the score as a field.
Use { $meta: "textScore" } in $sort or sort by the projected field name.
Users see documents ordered by how well they match the search terms.
The $meta operator connects full-text search to usable relevance ranking. With a text index, a $text query, and { $meta: "textScore" } in your projection, you can surface and sort by match quality.
For beginners, remember the three-part pattern: index, $text match, and $meta project/sort. Without all three, textScore will not work as expected.
$text and $metatextScore when users need ranked search results{ $meta: "textScore" } or the projected score fieldtitle, body, etc.)textScore without a $text query in the same operation$meta to work like a general expression operator$text expression is allowed per query$text search with MongoDB Atlas Search (different API)$metaUse these points when building text search in MongoDB.
Query extras.
PurposeRelevance value.
SyntaxSearch required.
PrerequisiteExpose & rank.
PatternCreate first.
SetupMove on to $millisecond for date part extraction, or review $map for array transforms.
5 people found this page helpful