翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$meta
$meta 集約演算子は、集約パイプライン内のドキュメントに関連付けられたメタデータにアクセスします。これは一般的に、テキスト検索スコアの取得と関連性による結果のソートに使用されます。
パラメータ
-
textScore: 検索クエリへのドキュメントの関連性を示すテキスト検索スコアを取得します。
例 (MongoDB シェル)
次の例は、集約パイプラインで $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 集計演算子を使用するコード例を表示するには、使用する言語のタブを選択します。