returnScope オプションを使用してクエリのコンテキストを設定し、オブジェクトの配列を個別のドキュメントとして返すことができます。
要件
returnScopeを使用してネストされたオブジェクトを個別のドキュメントとして検索するには、次の手順が必要です。
オブジェクトの配列を embeddedDocuments 型としてインデックス化します。
検索するネストされたフィールドの storedSource を定義します。MongoDB Search は、
storedSourceで定義されているフィールドのみを返します。クエリで returnStoredSource オプションを
trueに設定します。
構文
returnScope クエリでは、次の構文を使用します。
{ $search: { "<operator>": { <operator-specification>, }, "returnScope": { "path": "<embedded-documents-field-to-retrieve>" }, "returnStoredSource": true } }
{ $searchMeta: { "returnScope": { "path": "<embedded-documents-field-to-retrieve>" }, "returnStoredSource": true }, "facet": { "<operator>": { <operator-specification>, }, "facets": { <facet-definition> } } }
クエリ構文の詳細については、 $searchを参照してください。
動作
returnScopeオプションはクエリの取得コンテキストを設定します。クエリで returnScope を指定すると、MongoDB Search は各埋め込みドキュメントを個別のドキュメントであるかのようにスコア付け、ソート、カウントします。
Considerations
returnScope オプションを使用すると、MongoDB Search は embeddedDocument 内で storedSource として設定したフィールドのみを返します。embeddedDocument パスの外側のフィールド(ルートレベルのフィールドなど)および storedSource として設定されていないフィールドは返されません。
演算子仕様では、クエリするフィールドへの完全なパスを指定する必要があります。returnScope オプションを使用する場合は、すべての演算子仕様パスが returnScope.path の下にネストされていることを確認する必要があります。returnScope.path 外のフィールドをクエリするには、hasAncestor 演算子または hasRoot 演算子のいずれかを使用する必要があります。詳しくは、以下を参照してください。
ルート ドキュメント ID を検索する
クエリで returnScope を使用すると、MongoDB Search は searchRootDocumentId メタデータ フィールドを移入します。MongoDB 8.3 以降を実行しているクラスターでは、searchRootDocumentId メタデータ フィールドを使用できます。このフィールドには、返された各埋め込みドキュメントを含むルート ドキュメントの識別子が含まれています。この値をプロジェクトするには、searchRootDocumentId キーワードとともに$meta 式を使用します。
MongoDB Search は、クエリでreturnStoredSource: trueとreturnScope.pathの両方を設定した場合にのみsearchRootDocumentIdを移入します。returnScopeを指定しないクエリでsearchRootDocumentIdを参照すると、クエリは次のエラーで失敗します。
query requires $search root document id metadata, but it is not available
次の場合は、searchRootDocumentId を使用します。
子ドキュメントでフィルターした後、親ドキュメントからフィールドを取得します。
子ドキュメントを親でグループ化します。
searchRootDocumentId を使用しない方がよい場合は次のとおりです。
一致する子ドキュメントからのフィールドのみが必要です。
これはコストが高くなる可能性があるため、結果セット内のすべての子ドキュメントに対して親ドキュメントを返す必要があります。
例
以下の例はクエリで returnScope オプションの使用方法を示しています。この例は sample_training.companies サンプルデータセットを使用します。クラスターにデータをロードし、コレクションのフィールドにサンプルインデックスを作成したら、サンプルデータに対して次のクエリを試すことができます。
サンプル インデックス
1 { 2 "mappings": { 3 "dynamic": false, 4 "fields": { 5 "funding_rounds": { 6 "type": "embeddedDocuments", 7 "dynamic": true, 8 "fields": { 9 "investments": { 10 "type": "embeddedDocuments", 11 "dynamic": true 12 } 13 }, 14 "storedSource": { 15 "include": [ 16 "round_code", 17 "raised_currency_code", 18 "raised_amount", 19 "investments.person", 20 "investments.financial_org" 21 ] 22 } 23 } 24 } 25 } 26 }
上記のインデックス定義により、 MongoDB Search は次のように構成されます。
funding_roundsフィールドとfunding_rounds.investmentsフィールドをembeddedDocuments型としてインデックスします。funding_roundsとfunding_rounds.investmentsのオブジェクト配列にネストされたすべての動的にインデックス可能なフィールドをインデックスします。mongotに以下のフィールドを保存します。funding_rounds.round_codefunding_rounds.raised_currency_codefunding_rounds.raised_amountfunding_rounds.investments.personfunding_rounds.investments.financial_org
embeddedDocument演算子を使用すると、 funding_roundsフィールドとfunding_rounds.investmentsフィールドの両方に対して要素ごとのクエリを実行できます。次のセクションでは、returnScopeオプションを使用してembeddedDocumentsフィールドを個別のドキュメントとして検索するいくつかのサンプルクエリを示します。
{ ..., "funding_rounds": [ { "id": <integer>, "round_code": "<string>", "source_url": "<string>", "source_description": "<string>", "raised_amount": <integer>, "raised_currency_code": "<string>", "funded_year": <integer>, "funded_month": "<string>", "funded_day": "<string>", "investments": [ { "company": "<string>", "financial_org": { "name": "<string>", "permalink": "<string>" }, "person": { "first_name": "<string>", "last_name": "<string>", "permalink": "<string>" } }, ... ] }, ... ], ... }
サンプル クエリ
以下のセクションでは、returnScope オプションを使用してembeddedDocuments mongot型のフィールドで保存されたフィールドを検索するサンプルクエリを示します。