本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
$meta
$meta 運算子用於存取與目前查詢執行相關聯的中繼資料。此運算子主要用於文字搜尋操作,其中中繼資料可以提供有關相符文件相關性的資訊。
參數
-
textScore:擷取文件的文字搜尋分數。此分數表示文件與文字搜尋查詢的相關性。
範例 (MongoDB Shell)
下列範例示範如何使用 $meta運算子擷取符合文字搜尋查詢之文件的文字搜尋分數。
建立範例文件
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." } ]);
建立文字索引
db.documents.createIndex({ content: "text" });
查詢範例
db.documents.find( { $text: { $search: "coffee" } }, { _id: 0, title: 1, content: 1, score: { $meta: "textScore" } } ).sort({ score: { $meta: "textScore" } });
輸出
[
{
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
}
]
程式碼範例
若要檢視使用 $meta命令的程式碼範例,請選擇您要使用的語言標籤: