$meta
The $meta aggregation operator accesses metadata associated with documents in an aggregation pipeline. It is commonly used to retrieve text search scores and sort results by relevance.
Parameters
-
textScore: Retrieves the text search score indicating document relevance to the search query.
Example (MongoDB Shell)
The following example demonstrates using the $meta operator in an aggregation pipeline to retrieve and sort by text search scores.
Create sample documents
db.articles.createIndex({ content: "text" }); db.articles.insertMany([ { _id: 1, title: "Python Programming", content: "Python is a versatile programming language used for web development." }, { _id: 2, title: "Python Guide", content: "Learn Python programming with Python tutorials and Python examples." }, { _id: 3, title: "Java Basics", content: "Java is another popular programming language." } ]);
Query example
db.articles.aggregate([ { $match: { $text: { $search: "Python" } } }, { $addFields: { score: { $meta: "textScore" } } }, { $sort: { score: -1 } } ]);
Output
[
{
_id: 2,
title: 'Python Guide',
content: 'Learn Python programming with Python tutorials and Python examples.',
score: 1.5
},
{
_id: 1,
title: 'Python Programming',
content: 'Python is a versatile programming language used for web development.',
score: 0.75
}
]
Code examples
To view a code example for using the $meta aggregation operator, choose the tab for the language that you want to use: