Docs Menu
Docs Home
/ /

hasAncestor 演算子

hasAncestor

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

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

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

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

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

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

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}

次のクエリは、funding_rounds.founded_yearフィールドで2005年に設立された会社を検索し、equals演算子を使用して、金融組織や個人が会社に投資した詳細などの投資情報を検索します。

1db.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 }
]

戻る

geoWithin

項目一覧