$meta
The $meta operator is used to access metadata associated with the current query execution. This operator is primarily used for text search operations, where the metadata can provide information about the relevance of the matched documents.
Parameters
-
textScore: Retrieves the text search score for the document. This score indicates the relevance of the document to the text search query.
Example (MongoDB Shell)
The following example demonstrates how to use the $meta operator to retrieve the text search score for documents matching a text search query.
Create sample documents
db.documents.insertMany([ { _id: 1, title: "Coffee Basics", content: "Coffee is a popular beverage made from roasted coffee beans." }, { _id: 2, title: "Coffee Culture", content: "Coffee coffee coffee - the ultimate guide to coffee brewing and coffee preparation." }, { _id: 3, title: "Tea vs Coffee", content: "Many people prefer tea over coffee for its health benefits." } ]);
Create text index
db.documents.createIndex({ content: "text" });
Query example
db.documents.find( { $text: { $search: "coffee" } }, { _id: 0, title: 1, content: 1, score: { $meta: "textScore" } } ).sort({ score: { $meta: "textScore" } });
Output
[
{
title: 'Coffee Culture',
content: 'Coffee coffee coffee - the ultimate guide to coffee brewing and coffee preparation.',
score: 0.8897688388824463
},
{
title: 'Coffee Basics',
content: 'Coffee is a popular beverage made from roasted coffee beans.',
score: 0.75990891456604
},
{
title: 'Tea vs Coffee',
content: 'Many people prefer tea over coffee for its health benefits.',
score: 0.6079270839691162
}
]
Code examples
To view a code example for using the $meta command, choose the tab for the language that you want to use: