$meta
Returns metadata associated with documents in the result set, particularly useful for accessing text search scores and other query execution metadata.
Syntax
{ $meta: "<meta_data_keyword>" }
Parameters
meta_data_keyword
stringrequiredThe type of metadata to return: "textScore" for text search relevance scores
Examples
Get text search scores
Return text search relevance scores with query results
Query:
db.articles.find({ $text: { $search: "mongodb database" } }, { title: 1, score: { $meta: "textScore" } })
Output:
Documents with title field and search relevance score
Sort by text score
Sort text search results by relevance score
Query:
db.articles.find({ $text: { $search: "mongodb database" } }, { title: 1, score: { $meta: "textScore" } }).sort({ score: { $meta: "textScore" } })
Output:
Documents sorted by text search relevance, highest score first
Filter by minimum score
Return only documents with text score above threshold
Query:
db.articles.aggregate([{ $match: { $text: { $search: "mongodb database" } } }, { $addFields: { score: { $meta: "textScore" } } }, { $match: { score: { $gte: 0.5 } } }])
Output:
Documents with text search score of 0.5 or higher