定義
構文
hasRoot の構文は次のとおりです。
{ $search: { "index": "<index name>", // optional, defaults to "default" "hasRoot": { "operator": { <operator-specification> } }, "returnScope": { "path": "<embedded-documents-field-to-retrieve>" }, "returnStoredSource": true, }}フィールド
フィールド | タイプ | 説明 | 必要性 |
|---|---|---|---|
| オブジェクト | インデックス付きフィールドをクエリするために使用する演算子。これは、子ドキュメントをフィルターするための検索条件を指定します。 | 必須 |
要件
この演算子を使用するには、インデックス定義で次のことを行う必要があります。
クエリしたいルートレベルのフィールドにインデックスを作成します。
検索するオブジェクト配列を
embeddedDocuments型としてインデックスします。抽出するネストされたフィールドのstoredSourceも設定する必要があります。
クエリでは、次の操作を行う必要があります。
演算子を検索するフィールドのスコープを設定するには、
returnScopeを指定します。returnStoredSourceをtrueに設定してstoredSourceフィールドを返します。
例
このセクションの例では、sample_training.companies 名前空間を使用します。サンプル データセットをロードし、コレクションにインデックスを作成すると、このセクションで説明するサンプル クエリを試すことができます。
サンプル インデックス
このセクションのクエリは以下のインデックスを使用します。このインデックス定義により、 MongoDB Search は次の操作を実行するように構成されます。
コレクション内のすべての動的にインデックス付け可能なフィールドを自動的にインデックス化します。
products、funding_rounds、funding_rounds.investmentsフィールドを embeddedDocuments 型としてインデックス化します。mongotに以下のフィールドを保存します。funding_rounds.raised_currency_codefunding_rounds.raised_amountfunding_rounds.investmentsproducts.name
{ "mappings": { "dynamic": true, "fields": { "funding_rounds": { "dynamic": true, "fields": { "investments": [ { "dynamic": true, "type": "embeddedDocuments" } ] }, "storedSource": { "include": [ "raised_currency_code", "raised_amount", "investments" ] }, "type": "embeddedDocuments" }, "products": { "dynamic": true, "storedSource": { "include": [ "name" ] }, "type": "embeddedDocuments" } } }}簡単なクエリ
次のクエリは、ルートレベルのフィールドfounded_yearが2005年から2010年の間の年であるproductsを返します。
db.companies.aggregate([ { "$search": { "returnStoredSource": true, "returnScope": { "path": "products" }, "hasRoot": { "operator": { "range": { "path": "founded_year", "gte": 2005, "lte": 2010 } } } } }])マルチレベルクエリ
次のクエリは、nameフィールドでFacebookという名前の会社を検索し、text演算子を使用して、会社のすべての投資家(investments.personおよびinvestments.financial_org)による投資額(funding_rounds.raised_amountおよびfunding_rounds.raised_currency_code)を検索します。
db.companies.aggregate([ { "$search": { "returnStoredSource": true, "returnScope": { "path": "funding_rounds" }, "hasRoot": { "operator": { "text": { "path": "name", "query": "Facebook" } } } } }])複合クエリ
以下のクエリは、ネストされたfunding_rounds.investmentsフィールドでVenturesという用語(nameフィールド内の任意の場所)を持つ金融組織を検索し、networkという用語(descriptionフィールド内の任意の場所)を使用して説明されている会社に投資した組織を検索します。クエリは compound 演算子を使用して以下を検索します。
embeddedDocument演算子を使用するネストされたfunding_rounds.investments.financial_org.nameフィールドhasRoot演算子を使用するルートレベルのdescriptionフィールド
returnScopeは演算子のコンテキストをfunding_rounds embeddedDocuments型フィールドに設定し、returnStoredSourceフィールドはtrueに設定されてstoredSourceフィールドのみを返します。クエリは結果を5件のドキュメントに制限します。
db.companies.aggregate([ { "$search": { "compound": { "should": [ { "embeddedDocument": { "path": "funding_rounds.investments", "operator": { "wildcard": { "path": "funding_rounds.investments.financial_org.name", "query": "*Ventures*", "allowAnalyzedField": true } } } }, { "hasRoot": { "operator": { "wildcard": { "path": "description", "query": "*network*", "allowAnalyzedField": true } } } } ] }, "returnScope": {"path": "funding_rounds"}, "returnStoredSource": true } }, { "$limit": 5 }])