定義
構文
hasRoot の構文は次のとおりです。
1 { 2 $search: { 3 "index": "<index name>", // optional, defaults to "default" 4 "hasRoot": { 5 "operator": { 6 <operator-specification> 7 } 8 }, 9 "returnScope": { 10 "path": "<embedded-documents-field-to-retrieve>" 11 }, 12 "returnStoredSource": true, 13 } 14 }
フィールド
フィールド | タイプ | 説明 | 必要性 |
|---|---|---|---|
| オブジェクト | インデックス付きフィールドをクエリするために使用する演算子。これは、子ドキュメントをフィルターするための検索条件を指定します。 | 必須 |
要件
この演算子を使用するには、インデックス定義で次のことを行う必要があります。
クエリしたいルートレベルのフィールドにインデックスを作成します。
検索するオブジェクト配列を
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
1 { 2 "mappings": { 3 "dynamic": true, 4 "fields": { 5 "funding_rounds": { 6 "dynamic": true, 7 "fields": { 8 "investments": [ 9 { 10 "dynamic": true, 11 "type": "embeddedDocuments" 12 } 13 ] 14 }, 15 "storedSource": { 16 "include": [ 17 "raised_currency_code", 18 "raised_amount", 19 "investments" 20 ] 21 }, 22 "type": "embeddedDocuments" 23 }, 24 "products": { 25 "dynamic": true, 26 "storedSource": { 27 "include": [ 28 "name" 29 ] 30 }, 31 "type": "embeddedDocuments" 32 } 33 } 34 } 35 }
簡単なクエリ
次のクエリは、ルートレベルのフィールドfounded_yearが2005年から2010年の間の年であるproductsを返します。
1 db.companies.aggregate([ 2 { 3 "$search": { 4 "returnStoredSource": true, 5 "returnScope": { "path": "products" }, 6 "hasRoot": { 7 "operator": { 8 "range": { 9 "path": "founded_year", 10 "gte": 2005, 11 "lte": 2010 12 } 13 } 14 } 15 } 16 } 17 ])
マルチレベルクエリ
次のクエリは、nameフィールドでFacebookという名前の会社を検索し、text演算子を使用して、会社のすべての投資家(investments.personおよびinvestments.financial_org)による投資額(funding_rounds.raised_amountおよびfunding_rounds.raised_currency_code)を検索します。
1 db.companies.aggregate([ 2 { 3 "$search": { 4 "returnStoredSource": true, 5 "returnScope": { "path": "funding_rounds" }, 6 "hasRoot": { 7 "operator": { 8 "text": { 9 "path": "name", 10 "query": "Facebook" 11 } 12 } 13 } 14 } 15 } 16 ])
複合クエリ
以下のクエリは、ネストされた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件のドキュメントに制限します。
1 db.companies.aggregate([ 2 { 3 "$search": { 4 "compound": { 5 "should": [ 6 { 7 "embeddedDocument": { 8 "path": "funding_rounds.investments", 9 "operator": { 10 "wildcard": { 11 "path": "funding_rounds.investments.financial_org.name", 12 "query": "*Ventures*", 13 "allowAnalyzedField": true 14 } 15 } 16 } 17 }, 18 { 19 "hasRoot": { 20 "operator": { 21 "wildcard": { 22 "path": "description", 23 "query": "*network*", 24 "allowAnalyzedField": true 25 } 26 } 27 } 28 } 29 ] 30 }, 31 "returnScope": {"path": "funding_rounds"}, 32 "returnStoredSource": true 33 } 34 }, 35 { 36 "$limit": 5 37 } 38 ])