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

オブジェクト

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

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

必須

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

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

  • クエリおよび検索するドキュメントの範囲を設定するには、 returnScopes を指定します。

このセクションの例では、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}

次のクエリは、equals 演算子を使用して、2005 年に開始された会社を funding_rounds.founded_yearフィールドで検索し、会社にインデックスを発行した金融組織や個人などのコストの詳細を取得します。

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

項目一覧