本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
$meta
$meta 彙總運算子會存取與彙總管道中文件相關聯的中繼資料。它通常用於擷取文字搜尋分數,並依相關性排序結果。
參數
-
textScore:擷取文字搜尋分數,指出與搜尋查詢的文件相關性。
範例 (MongoDB Shell)
下列範例示範在彙總管道中使用 $meta 運算子,依文字搜尋分數擷取和排序。
建立範例文件
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." } ]);
查詢範例
db.articles.aggregate([ { $match: { $text: { $search: "Python" } } }, { $addFields: { score: { $meta: "textScore" } } }, { $sort: { score: -1 } } ]);
輸出
[
{
_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
}
]
程式碼範例
若要檢視使用$meta彙總運算子的程式碼範例,請選擇您要使用的語言標籤: