MongoDB $meta Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $meta?

$meta returns metadata about a document—not a normal field. The value beginners use most is "textScore": how well a document matched a $text search. Higher score = better match.

Remember
$meta = read document metadata

{ $meta: "textScore" }

needs:
  1) text index
  2) $text query / $match
  3) then $meta in project or sort

higher textScore → better match
sort by textScore → best first
UsePrefer
Relevance from legacy $text{ $meta: "textScore" }
Run the text search itself$text
Atlas Search relevance$search + searchScore

How to Use $meta in MongoDB

Create a text index, search with $text, then project or sort the score.

  1. Create a text index on the field(s) you search.
  2. Query with $text (find) or $match + $text (aggregation).
  3. Project the score: { score: { $meta: "textScore" } }.
  4. Optionally $sort by { score: { $meta: "textScore" } } for best matches first.
PieceRequired?Role
Text indexYes (for textScore)Enables $text search
$textYes (for textScore)Produces the relevance metadata
$meta: "textScore"Yes (to read it)Exposes or sorts by the score

Pseudocode

Pseudocode
ensure text index on searchable fields

for each document matching $text search:
    score = metadata.textScore   // relevance
    emit {
      ...,
      score: score               // via $meta
    }

optional:
    sort by textScore descending // best first

Cheat Sheet

Keep these facts handy while reading search metadata.

TopicDetail
Syntax{ $meta: "<keyword>" }
Common keyword"textScore"
Project{ score: { $meta: "textScore" } }
Sort{ score: { $meta: "textScore" } } (desc by default)
PrerequisiteText index + $text in the query
Debug keyword"indexKey" (not for app logic)
Atlas Search"searchScore" after $search

Live Preview

Simulate a $text search for "cake" and see $meta: "textScore" ranking (sample values).

Simulates $text: "cake" + { $meta: "textScore" } on the articles sample below.

Result
$text search: "cake"

{ _id: 4, title: "some cakes", score: 1 }
{ _id: 2, title: "more cakes", score: 1 }
{ _id: 1, title: "cakes and ale", score: 0.75 }
{ _id: 5, title: "two cakes to go", score: 0.666667 }

Worked Walkthrough — Search “cake”

Articles have titles like "some cakes" and "bread". A text search for "cake" matches cake titles and scores them.

Calculation
Text index on: title
Query: { $text: { $search: "cake" } }

Matches (examples):
  "some cakes"      → score 1
  "more cakes"      → score 1
  "cakes and ale"   → score 0.75
  "two cakes to go" → score ≈ 0.67

Non-matches:
  "bread", "pie"    → not returned

$meta: "textScore" exposes those scores
$sort by textScore → best matches first

MongoDB Examples

We use a small articles collection. Create a text index once, then project and sort relevance with $meta. Run these in mongosh.

Example 1 — Sample Data + Text Index

Imagine six short article titles. We save them in articles, then create a text index on title.

mongosh
db.articles.insertMany([
  { _id: 1, title: "cakes and ale" },
  { _id: 2, title: "more cakes" },
  { _id: 3, title: "bread" },
  { _id: 4, title: "some cakes" },
  { _id: 5, title: "two cakes to go" },
  { _id: 6, title: "pie" }
]);

db.articles.createIndex({ title: "text" });

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps six documents. Run find() to see them:

mongosh
db.articles.find()

Read the setup like this:

PieceValuePlain English
titlestringField we will search
Text index{ title: "text" }Required for $text + textScore

How It Works

  1. Insert once. Six title documents land in articles.
  2. Create a text index. createIndex({ title: "text" }) enables full-text search on titles.
  3. Ready for $meta. Without this index, $text and textScore cannot run.

Example 2 — Project textScore

Goal: search for "cake" and show each match’s relevance with $meta.

mongosh
db.articles.aggregate([
  { $match: { $text: { $search: "cake" } } },
  {
    $project: {
      title: 1,
      score: { $meta: "textScore" }
    }
  }
]);

Same idea with find():

mongosh
db.articles.find(
  { $text: { $search: "cake" } },
  { score: { $meta: "textScore" } }
);

How It Works

  1. $match + $text. Keeps only titles that match the word cake.
  2. $meta reads metadata. { $meta: "textScore" } copies the relevance score into score.
  3. Order may vary. Without an explicit sort, match order is not guaranteed to be best-first.
  4. Non-matches stay out. "bread" and "pie" do not appear.

Example 3 — Sort by textScore

Same search, but rank results so the best matches come first.

mongosh
db.articles.aggregate([
  { $match: { $text: { $search: "cake" } } },
  {
    $addFields: {
      score: { $meta: "textScore" }
    }
  },
  { $sort: { score: { $meta: "textScore" } } }
]);

How It Works

  1. Same $text match. Still searching for "cake" on the indexed titles.
  2. Expose the score. $addFields + $meta stores relevance in score.
  3. Sort by metadata. $sort with { $meta: "textScore" } ranks highest scores first (descending by default).
  4. Ties are fine. Both "some cakes" and "more cakes" can share score 1.

Edge Cases & Pitfalls

Check these before using $meta in production pipelines.

$text

textScore needs $text

Without a $text query (or $match with $text), { $meta: "textScore" } fails in modern MongoDB.

index

Create the text index first

No text index → no $text search. One text index per collection (covering the fields you need).

atlas

Don’t mix up searchScore

textScore is for legacy $text. Atlas Search uses $search and { $meta: "searchScore" }.

debug

indexKey is for debugging

{ $meta: "indexKey" } shows which index key was used. Do not build app logic on it—plans can change.

Performance Notes

ApproachCostBest for
$text + $metaUses text indexSimple keyword search + ranking
Project score onlyCheap per matchShowing relevance to users
Sort by textScoreSorts the match setBest-first result lists

Text search cost is dominated by the index and match set size, not by reading $meta. Prefer $search on Atlas for richer full-text needs.

Key Takeaways

  • Role: read metadata like textScore.
  • Needs: text index + $text first.
  • Pattern: project or sort with { $meta: "textScore" }.
  • Atlas: use searchScore with $search.

One line: $meta answers “how well did this document match the text search?”—via textScore.

Frequently Asked Questions

$meta returns metadata about a document—most often "textScore", the relevance score from a $text full-text search. You can also use keywords like "indexKey" (debug) or Atlas Search scores such as "searchScore".
The syntax is { $meta: "<keyword>" }. For text search relevance, use { $meta: "textScore" } inside a projection, $project, $addFields, or $sort—together with a $text query.
You need a text index, a query (or $match) that includes $text, and then { $meta: "textScore" }. Without $text in the query, textScore is not available and the operation fails in modern MongoDB versions.
Yes. In find(), put { score: { $meta: "textScore" } } in the projection, and optionally sort with { score: { $meta: "textScore" } }. In aggregation, use the same form after a $match with $text.
Sort with { score: { $meta: "textScore" } }. textScore sorts descending by default (best matches first). You can sort without projecting the score.
No. textScore comes from legacy $text + a text index. On Atlas Search, use $search and { $meta: "searchScore" } instead.

Next: $millisecond

Get the 0–999 millisecond portion of a date in aggregation.

$millisecond tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful