定義
構文
hasAncestor演算子の構文は次のとおりです。
1 { 2 $search: { 3 "index": "<index name>", // optional, defaults to "default" 4 "hasAncestor": { 5 "ancestorPath": "<embeddedDocuments-type-higher-level-field>", 6 "operator": { 7 <operator-specification> 8 } 9 }, 10 "returnScope": { 11 "path": "<embeddedDocuments-type-field-to-retrieve>" 12 } 13 } 14 }
フィールド
フィールド | タイプ | 説明 | 必要性 |
|---|---|---|---|
| 文字列 | クエリで使用されるネストされたフィールドの中間祖先である | 必須 |
| オブジェクト |
演算子仕様 内では | 必須 |
要件
この演算子を使用するには、次の条件を満たす必要があります。
祖先フィールドを embeddedDocuments
ancestorPath型としてインデックス化します。クエリで、このフィールドとして を指定します。フィールドがルート レベルにある場合は、 hasRoot を使用します。クエリおよび検索するドキュメントの範囲を設定するには、 returnScopes を指定します。
例
このセクションの例では、sample_training.companies データセットを使用します。サンプルデータセット をロードし、コレクションにインデックスを作成 したら、このセクションで説明されるサンプルクエリを試すことができます。
サンプル インデックス
このセクションのクエリでは、次のインデックスを使用します。このインデックス定義により、 MongoDB Search は次の操作を実行するように構成されます。
コレクション内のすべての動的にインデックス付け可能なフィールドを自動的にインデックス化します。
フィールドと
funding_roundsfunding_rounds.investmentsフィールドを embeddedDocuments 型としてインデックス化します。次のフィールドを
mongotに保存します。funding_rounds.investments.financial_orgfunding_rounds.investments.person
1 { 2 "mappings": { 3 "dynamic": true, 4 "fields": { 5 "funding_rounds": { 6 "dynamic": true, 7 "fields": { 8 "investments": [ 9 { 10 "dynamic": true, 11 "storedSource": { 12 "include": [ 13 "financial_org", 14 "person" 15 ] 16 }, 17 "type": "embeddedDocuments" 18 } 19 ] 20 }, 21 "type": "embeddedDocuments" 22 } 23 } 24 } 25 }
サンプル クエリ
次のクエリは、equals 演算子を使用して、2005 年に開始された会社を funding_rounds.founded_yearフィールドで検索し、会社にインデックスを発行した金融組織や個人などのコストの詳細を取得します。
1 db.companies.aggregate([ 2 { 3 "$search": { 4 "returnStoredSource": true, 5 "returnScope": { "path": "funding_rounds.investments" }, 6 "hasAncestor": { 7 "ancestorPath": "funding_rounds", 8 "operator": { 9 "equals": { 10 "path": "funding_rounds.funded_year", 11 "value": 2005 12 } 13 } 14 } 15 } 16 } 17 ])
[ { financial_org: { name: 'Frazier Technology Ventures', permalink: 'frazier-technology-ventures' }, person: null }, { financial_org: { name: 'Trinity Ventures', permalink: 'trinity-ventures' }, person: null }, { financial_org: { name: 'Accel Partners', permalink: 'accel-partners' }, person: null }, { financial_org: null, person: { first_name: 'Mark', last_name: 'Pincus', permalink: 'mark-pincus' } }, { financial_org: null, person: { first_name: 'Reid', last_name: 'Hoffman', permalink: 'reid-hoffman' } }, { financial_org: { name: 'First Round Capital', permalink: 'first-round-capital' }, person: null }, { financial_org: null, person: { first_name: 'Ram', last_name: 'Shriram', permalink: 'ram-shriram' } }, { financial_org: null, person: { first_name: 'Mitch', last_name: 'Kapor', permalink: 'mitch-kapor' } }, { financial_org: null, person: { first_name: 'Ron', last_name: 'Conway', permalink: 'ron-conway' } }, { financial_org: null, person: { first_name: 'Silvio', last_name: 'Scaglia', permalink: 'silvio-scaglia' } }, { financial_org: { name: 'Shelter Capital Partners', permalink: 'shelter-capital-partners' }, person: null }, { financial_org: { name: 'First Round Capital', permalink: 'first-round-capital' }, person: null }, { financial_org: { name: 'Liberty Associated Partners', permalink: 'liberty-associated-partners' }, person: null }, { financial_org: { name: 'Rose Tech Ventures', permalink: 'rose-tech-ventures' }, person: null }, { financial_org: { name: 'First Round Capital', permalink: 'first-round-capital' }, person: null }, { financial_org: { name: 'Lead Dog Ventures', permalink: 'lead-dog-ventures' }, person: null }, { financial_org: { name: 'Accel Partners', permalink: 'accel-partners' }, person: null }, { financial_org: { name: 'Benchmark', permalink: 'benchmark-2' }, person: null }, { financial_org: { name: 'Sequoia Capital', permalink: 'sequoia-capital' }, person: null }, { financial_org: null, person: null } ]