定義
構文
hasAncestor演算子の構文は次のとおりです。
{ $search: { "index": "<index name>", // optional, defaults to "default" "hasAncestor": { "ancestorPath": "<embeddedDocuments-type-higher-level-field>", "operator": { <operator-specification> } }, "returnScope": { "path": "<embeddedDocuments-type-field-to-retrieve>" } }}フィールド
フィールド | タイプ | 説明 | 必要性 |
|---|---|---|---|
| 文字列 | クエリで使用されるネストされたフィールドの中間祖先である | 必須 |
| オブジェクト |
| 必須 |
要件
この演算子を使用するには、以下の条件を満たす必要があります。
祖先フィールドをembeddedDocuments型としてインデックスします。クエリで、このフィールドとして
ancestorPathを指定します。フィールドがルート レベルにある場合は、hasRoot を使用します。returnScope を指定して、クエリおよび検索するドキュメントのスコープを設定します。
例
このセクションの例では、sample_training.companies データセットを使用します。サンプル データセットをロードし、コレクションにインデックスを作成すると、このセクションで説明するサンプル クエリを試すことができます。
サンプル インデックス
このセクションのクエリは以下のインデックスを使用します。このインデックス定義により、 MongoDB Search は次の操作を実行するように構成されます。
コレクション内のすべての動的にインデックス付け可能なフィールドを自動的にインデックス化します。
funding_roundsフィールドとfunding_rounds.investmentsフィールドをembeddedDocuments 型としてインデックスします。mongotに以下のフィールドを保存します。funding_rounds.investments.financial_orgfunding_rounds.investments.person
{ "mappings": { "dynamic": true, "fields": { "funding_rounds": { "dynamic": true, "fields": { "investments": [ { "dynamic": true, "storedSource": { "include": [ "financial_org", "person" ] }, "type": "embeddedDocuments" } ] }, "type": "embeddedDocuments" } } }}サンプル クエリ
次のクエリは、funding_rounds.founded_yearフィールドで2005年に設立された会社を検索し、equals演算子を使用して、金融組織や個人が会社に投資した詳細などの投資情報を検索します。
db.companies.aggregate([ { "$search": { "returnStoredSource": true, "returnScope": { "path": "funding_rounds.investments" }, "hasAncestor": { "ancestorPath": "funding_rounds", "operator": { "equals": { "path": "funding_rounds.funded_year", "value": 2005 } } } } }])[ { 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 }]