For AI agents: a documentation index is available at https://www.mongodb.com/ko-kr/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs Menu

hasAncestor 연산자

hasAncestor

hasAncestor 연산자는 ancestorPath에 지정된 embeddedDocuments 유형의 필드를 쿼리합니다. ancestorPathreturnScope에서 지정한 필드의 부모 필드입니다. 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 연산자를 사용할 수 없습니다.

필수 사항

이 연산자를 사용하려면 다음을 수행해야 합니다.

  • 앤세스터 필드를 embeddedDocuments 유형으로 인덱스합니다. 쿼리 에서 이 필드 로 ancestorPath 를 지정합니다. 필드 루트 수준에 있는 경우 hasRoot를 사용합니다.

  • returnScope 를 지정하여 쿼리 및 조회할 문서의 범위를 설정합니다.

이 섹션의 예시에서는 sample_training.companies 데이터세트를 사용합니다. 샘플 데이터 세트를 로드하고 컬렉션에 인덱스를 생성하면 이 섹션에서 설명하는 샘플 쿼리를 실행할 수 있습니다.

이 섹션의 쿼리에서는 다음 인덱스를 사용합니다. 이 인덱스 정의는 MongoDB 검색을 다음 작업을 수행하도록 구성합니다.

  • 컬렉션에서 동적으로 인덱싱할 수 있는 모든 필드를 자동으로 인덱싱합니다.

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