정의
구문
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를 사용합니다.
- 쿼리 및 조회 할 문서 의 범위를 설정하다 하려면 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 } 
샘플 쿼리
다음 쿼리 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 } ]