AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

hasAncestor 演算子

hasAncestor

hasAncestor演算子は、ancestorPathで指定したembeddedDocuments型フィールドをクエリします。ancestorPath は、returnScope で指定したフィールドの親です。returnScopeは、抽出するネストされたフィールド(ネストのレベルを含む)を指定します。

この演算子は、配列内のオブジェクトを独立したドキュメントとしてスコアリングして取得するときに使用して、次のことを行うことができます。

  • 祖先フィールド(より高いネストレベル)にフィルターします。

  • 兄弟フィールド(同じネストレベル)にフィルターします。

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>" } }}
フィールド
タイプ
説明
必要性

ancestorPath

文字列

クエリで使用されるネストされたフィールドの中間祖先である embeddedDocuments 型フィールドの名前。これは、 演算子が評価する親ドキュメントです。

必須

operator

オブジェクト

embeddedDocuments 型のフィールドで指定された ancestorPath 内のネストされたフィールドをクエリするために使用する演算子。ネストのレベルに関係なく、クエリパスは ancestorPath の子である必要があります。親ドキュメントをフィルタリングする検索条件を指定します。

hasAncestor演算子は演算子仕様内で使用することはできません。

必須

この演算子を使用するには、以下の条件を満たす必要があります。

  • 祖先フィールドをembeddedDocuments型としてインデックスします。クエリで、このフィールドとして ancestorPath を指定します。フィールドがルート レベルにある場合は、hasRoot を使用します。

  • returnScope を指定して、クエリおよび検索するドキュメントのスコープを設定します。

このセクションの例では、sample_training.companies データセットを使用します。サンプル データセットをロードし、コレクションにインデックスを作成すると、このセクションで説明するサンプル クエリを試すことができます。

このセクションのクエリは以下のインデックスを使用します。このインデックス定義により、 MongoDB Search は次の操作を実行するように構成されます。

  • コレクション内のすべての動的にインデックス付け可能なフィールドを自動的にインデックス化します。

  • funding_roundsフィールドとfunding_rounds.investmentsフィールドをembeddedDocuments 型としてインデックスします。

  • mongot に以下のフィールドを保存します。

    • funding_rounds.investments.financial_org

    • funding_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 }]