定義
$metaドキュメントに関連付けられたメタデータを返します。例:テキスト検索を実行すると返される
"textScore"。A
$metaexpression has the following syntax:{ $meta: <metaDataKeyword> } The
$metaexpression can specify the following values as the<metaDataKeyword>:Keyword説明"textScore"一致するドキュメントごとに、対応する
$textクエリに関連付けられたスコアを返します。テキスト スコアは、ドキュメントが 1 つまたは複数の検索タームにどの程度一致したかを示します。{ $meta: "textScore" }$textクエリと組み合わせて使う必要があります。前のバージョンでは、
$textクエリと組み合わせて使用されない場合、null スコアが返されます。$textは、自己管理型(Atlas 以外)配置に対するテキスト クエリ機能を提供します。MongoDB Atlas でホストされているデータに対して、MongoDB は改良された全文クエリ ソリューションである Atlas Search を提供します。"indexKey"テキスト以外のインデックスが使用されている場合、ドキュメントのインデックス キーを返します。
{ $meta: "indexKey" }式はデバッグ目的のみで使用され、アプリケーション ロジックには使用されず、cursor.returnKey()よりも優先されます。MongoDB Atlas Search provides additional
$metakeywords, such as:"searchSequenceToken" ( MongoDB.. および.. 以降で利用可能7 056013)
詳細については、Atlas Search のドキュメントを参照してください。
重要
次の
$metaキーワードは、Stable API V1 でサポートされていません。"textScore""indexKey""searchScore""searchHighlights"(MongoDB 7.0.5 および 6.0.13 から利用可能)"searchSequenceToken""searchScoreDetails"
動作
テキスト スコア メタデータ $meta: "textScore"
$text 検索が必要です
{ $meta: "textScore" }式は$textと組み合わせて使用する必要があります。以下に例を挙げます。find では、
{ $meta: "textScore" }を使用するには、クエリ述語に$text演算子を指定する必要があります。クエリ述語に$text演算子を指定しないと、操作は失敗します。
注意
$textは、自己管理型(Atlas 以外)配置に対するテキスト クエリ機能を提供します。MongoDB Atlas でホストされているデータに対して、MongoDB は改良された全文クエリ ソリューションである Atlas Search を提供します。
可用性
プロジェクションでの使用
テキスト スコアでフィルタリング
集計では、テキスト スコア値を含むフィールドを出力するステージに続く以降のステージにおいて、クエリ条件を指定したり、そのフィールド上で操作したりできます。 たとえば、「自己管理型配置の集計パイプラインの $text 」を参照してください。
find では、テキスト スコアにクエリ条件を指定することはできません。代わりに集計を使用してください。
ソートでの使用
"textScore"メタデータは降順でソートされます。ソート操作で使用するには、
{ $meta: "textScore" }式を任意のフィールド名に設定します。フィールド名はクエリ システムによって無視されます。
プロジェクションを使わないソート
集計では、
textScoreをプロジェクトしなくても、結果のドキュメントを{ $meta: "textScore" }でソートできます。find では、
textScoreをプロジェクションしなくても、結果のドキュメントを{ $meta: "textScore" }でソートすることができます。
プロジェクションによるソート
In aggregation, if you include the
{ $meta: "textScore" }expression in both the projection and sort, the projection and sort can have different field names for the expression. The field name in the sort is disregarded by the query system.In find, if you include the
{ $meta: "textScore" }expression in both the projection and sort, the projection and sort can have different field names for the expression. The field name in the sort is disregarded by the query system.
インデックス キー メタデータ $meta: "indexKey"(集計と検索)
使用法
{ $meta: "indexKey" }式はデバッグ目的のみで使用され、アプリケーション ロジックには使用できません。{ $meta: "indexKey" }式はcursor.returnKey()よりも優先されます。
可用性
集計では、
{ $meta: "indexKey" }式は、$project、$group、$sortByCountなどの集計式を受け入れるさまざまなステージに含めることができますが、$sortには含めることができません。ただし、集計パイプラインを使用すると、最初に{ $meta: "indexKey" }式($project、$addFieldsなど)をプロジェクトし、その後、後続の$sortステージでそのフィールドによってソートできます。find では、
{ $meta: "indexKey" }式はプロジェクション ドキュメントの一部としてのみ使用できます。
戻り値
返される値は、データベースがインデックスの値を表す方法によって異なり、バージョンによって変わる場合があります。表される値は、フィールドの実際の値と異なる場合があります。
返される値は、システムによって選択された実行プランによって異なります。たとえば、クエリに答えるために使用できるインデックスが 2 つある場合、"indexKey" メタデータの値は、選択されているインデックスによって異なります。
インデックスを使用しない場合、
{ $meta: "indexKey" }式は値を返さず、フィールドは出力に含まれません。
例
$meta: "textScore"
次のドキュメントを使用して articles コレクションを作成します。
db.articles.insertMany([ { "_id" : 1, "title" : "cakes and ale" }, { "_id" : 2, "title" : "more cakes" }, { "_id" : 3, "title" : "bread" }, { "_id" : 4, "title" : "some cakes" }, { "_id" : 5, "title" : "two cakes to go" }, { "_id" : 6, "title" : "pie" } ])
title フィールドにテキスト インデックスを作成します。
db.articles.createIndex( { title: "text"} )
The following aggregation operation performs a text search and uses the $meta operator to group by the text search score:
db.articles.aggregate( [ { $match: { $text: { $search: "cake" } } }, { $group: { _id: { $meta: "textScore" }, count: { $sum: 1 } } } ] )
この操作は次の結果を返します。
{ "_id" : 0.75, "count" : 1 } { "_id" : 0.6666666666666666, "count" : 1 } { "_id" : 1, "count" : 2 }
その他の例については、「自己管理型配置の集計パイプラインの $text 」を参照してください。
The following query performs a text search for the term cake and uses the $meta operator in the projection document to include the score assigned to each matching document:
db.articles.find( { $text: { $search: "cake" } }, { score: { $meta: "textScore" } } )
この操作では、テキスト スコアを含む次のドキュメントが返されます。
{ "_id" : 4, "title" : "some cakes", "score" : 1 } { "_id" : 1, "title" : "cakes and ale", "score" : 0.75 } { "_id" : 5, "title" : "two cakes to go", "score" : 0.6666666666666666 } { "_id" : 2, "title" : "more cakes", "score" : 1 }
"textScore" のプロジェクションとソートの追加の例については、「関連性スコアの例」を参照してください。
$meta: "indexKey"
注意
{ $meta: "indexKey" } 式はデバッグ目的のみで使用され、アプリケーション ロジックには使用できません。MongoDB は、クエリ システムによって選択されたインデックスに関連付けられた値を返します。システムは、その後の実行時に別のインデックスを選択することができます。
選択したインデックスで返される値は、データベースがインデックスの値を表す方法によって異なり、バージョンによって変わる場合があります。表される値は、フィールドの実際の値と異なる場合があります。
次のドキュメントを使用して orders コレクションを作成します。
db.orders.insertMany([ { "item" : "abc", "price" : Decimal128("12"), "quantity" : 2, "type": "apparel" }, { "item" : "jkl", "price" : Decimal128("20"), "quantity" : 1, "type": "electronics" }, { "item" : "abc", "price" : Decimal128("10"), "quantity" : 5, "type": "apparel" } ])
type フィールドと item フィールドに次の複合インデックスを作成します。
db.orders.createIndex( { type: 1, item: 1 } )
The following aggregation operation finds all documents with type equal to apparel and uses the $meta operator to include the index key value for the matching document if an index was used:
db.orders.aggregate( [ { $match: { type: "apparel" } }, { $addFields: { idxKey: { $meta: "indexKey" } } } ] )
The following operation finds all documents with type equal to apparel and uses the $meta operator to include the index key value for the matching document if an index was used:
db.orders.find( { type: "apparel" }, { idxKey: { $meta: "indexKey" } } )
この操作では、対応するインデックス キーを持つ一致するドキュメントが返されます。
{ "_id" : ObjectId("5e98a33ceaf5e9dcf2b8dcde"), "item" : "abc", "price" : Decimal128("12"), "quantity" : 2, "type" : "apparel", "idxKey" : { "type" : "apparel", "item" : "abc" } } { "_id" : ObjectId("5e98a33ceaf5e9dcf2b8dce0"), "item" : "abc", "price" : Decimal128("10"), "quantity" : 5, "type" : "apparel", "idxKey" : { "type" : "apparel", "item" : "abc" } }
インデックスが使用されていない場合、{ $meta: "indexKey" } は何も返しません。
例えば次の操作では、一致条件をサポートするインデックスがpriceフィールドに存在しないため、インデックスは使用されません。
db.orders.aggregate( [ { $match: { price: { $gte: Decimal128("10") } } }, { $addFields: { idxKey: { $meta: "indexKey" } } } ] )
例えば次の操作では、一致条件をサポートするインデックスがpriceフィールドに存在しないため、インデックスは使用されません。
db.orders.find( { price: { $gte: Decimal128("10") } }, { idxKey: { $meta: "indexKey" } } )
この操作では、idxKey フィールドのない一致するドキュメントが返されます。
{ "_id" : ObjectId("5e98a33ceaf5e9dcf2b8dcde"), "item" : "abc", "price" : NumberDecimal("12"), "quantity" : 2, "type" : "apparel" } { "_id" : ObjectId("5e98a33ceaf5e9dcf2b8dcdf"), "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : 1, "type" : "electronics" } { "_id" : ObjectId("5e98a33ceaf5e9dcf2b8dce0"), "item" : "abc", "price" : NumberDecimal("10"), "quantity" : 5, "type" : "apparel" }
このページのC#の例では、Atlasサンプルデータセット の sample_mflixデータベースを使用します。MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 MongoDB .NET/ C#ドライバーのドキュメントの「 開始 」を参照してください。
次の Movie クラスは、sample_mflix.movies コレクション内のドキュメントをモデル化します。
public class Movie { public ObjectId Id { get; set; } public string Plot { get; set; } public List<string> Genres { get; set; } public int Runtime { get; set; } public List<string> Cast { get; set; } public string Title { get; set; } [] public DateTime LastUpdated { get; set; } public string Rated { get; set; } public int Year { get; set; } public ImdbData Imdb { get; set; } public string Type { get; set; } public int Index { get; set; } public string[] Comments { get; set; } public List<BsonDocument> Highlights { get; set; } public float Score { get; set; } [] public SearchScoreDetails ScoreDetails { get; set; } [] public SearchScoreDetails SearchScoreDetails { get; set; } [] public string PaginationToken { get; set; } [] public float[] PlotEmbedding { get; set; } }
注意
パスカルケースの ConventionPack
このページのC# クラスはプロパティ名にパスカルケースを使用していますが、MongoDB コレクションのフィールド名はキャメルケースを使用しています。この違いを考慮するために、アプリケーションが起動する際に次のコードを使用してConventionPackを登録してください。
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() }; ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
To include the text search score by using the .NET/C# driver, call the MetaTextScore() method on the projection builder.
まず、次のコードを実行して、Title フィールドにテキスト インデックスを作成してください。
var indexModel = new CreateIndexModel<Movie>( Builders<Movie>.IndexKeys.Text(m => m.Title)); movieCollection.Indexes.CreateOne(indexModel);
次に、以下のコードをアプリケーションに追加します。このコードでは、"future" という語句に対するテキスト検索を実行し、MetaTextScore() メソッドを使用して、各一致ドキュメントに割り当てられたスコアを含めます。次にコードは、テキスト スコアに基づいて結果を降順に並べ替え、最もスコアが高いドキュメントを返します。
var filter = Builders<Movie>.Filter.Text("future"); var projection = Builders<Movie>.Projection .Include(m => m.Title) .Include(m => m.Plot) .MetaTextScore(m => m.Score); var result = movieCollection.Find(filter) .Sort(Builders<Movie>.Sort.MetaTextScore("score")) .Project(projection) .FirstOrDefault();
前の操作では、次のドキュメントが返されます。
{ "_id": { "$oid": "573a1398f29313caabceb500" }, "plot": "After visiting 2015, Marty McFly must repeat his visit to 1955 to prevent disastrous changes to 1985... without interfering with his first trip.", "title": "Back to the Future Part II", "score": 1.59375 }