정의
구문
hasRoot 의 구문은 다음과 같습니다:
1 { 2   $search: { 3     "index": "<index name>", // optional, defaults to "default" 4     "hasRoot": { 5       "operator": { 6         <operator-specification> 7       } 8     }, 9     "returnScope": { 10       "path": "<embedded-documents-field-to-retrieve>" 11     }, 12     "returnStoredSource": true, 13   } 14 } 
필드
| 필드 | 유형 | 설명 | 필요성 | 
|---|---|---|---|
| 
 | 객체 | 인덱싱된 필드 쿼리 데 사용하는 연산자입니다. 하위 문서를 필터하다 위한 검색 기준을 지정합니다. | 필수 사항 | 
요구 사항:
이 연산자 사용하려면 인덱스 정의에서 다음을 수행해야 합니다.
- 쿼리 하려는 루트 수준 필드 인덱싱합니다. 
- embeddedDocuments유형으로 조회 하려는 객체 배열 인덱싱합니다. 조회 하려는 중첩 필드에 대해- storedSource도 구성해야 합니다.
쿼리 에서 다음을 수행해야 합니다.
- 연산자 를 검색 할 필드 범위를 설정하다 하려면 - returnScope를 지정합니다.
- returnStoredSource를- true로 설정하여- storedSource필드를 반환합니다.
예시
이 섹션의 예제에서는 sample_training.companies 네임스페이스 사용합니다. 샘플 데이터 세트를 로드하고 컬렉션 에 인덱스 생성하는 경우 이 섹션에 설명된 샘플 쿼리를 시도할 수 있습니다.
샘플 인덱스
이 섹션의 쿼리는 다음 인덱스 사용합니다. 이 인덱스 정의는 다음을 수행하도록 MongoDB Search를 구성합니다.
- 컬렉션에서 동적으로 인덱싱할 수 있는 모든 필드를 자동으로 인덱싱합니다. 
- products- funding_rounds, 및- funding_rounds.investments필드를 embeddedDocuments 유형으로 인덱싱합니다.
- 다음 필드를 - mongot에 저장합니다.- funding_rounds.raised_currency_code
- funding_rounds.raised_amount
- funding_rounds.investments
- products.name
 
1 { 2   "mappings": { 3     "dynamic": true, 4     "fields": { 5       "funding_rounds": { 6         "dynamic": true, 7         "fields": { 8           "investments": [ 9             { 10               "dynamic": true, 11               "type": "embeddedDocuments" 12             } 13           ] 14         }, 15         "storedSource": { 16           "include": [ 17             "raised_currency_code", 18             "raised_amount", 19             "investments" 20           ] 21         }, 22         "type": "embeddedDocuments" 23       }, 24       "products": { 25         "dynamic": true, 26         "storedSource": { 27           "include": [ 28             "name" 29           ] 30         }, 31         "type": "embeddedDocuments" 32       } 33     } 34   } 35 } 
단순 쿼리
다음 쿼리 루트 수준 필드 founded_year 가 2005 년과 2010년 사이에 있는 products 을 반환합니다.
1 db.companies.aggregate([ 2   { 3     "$search": {  4       "returnStoredSource": true, 5       "returnScope": { "path": "products" }, 6       "hasRoot": { 7         "operator": { 8           "range": { 9             "path": "founded_year", 10             "gte": 2005, 11             "lte": 2010 12           } 13         } 14       } 15     } 16   } 17 ]) 
[   { name: 'Wikison Wetpaint' },   { name: 'Wetpaint Social Distribution System' },   { name: 'Omnidrive' },   { name: 'Twitter Blocks' },   { name: 'Twicco' },   { name: 'Slacker Web Player' },   { name: 'Slacker Desktop Radio' },   { name: 'Slacker Portable Player' },   { name: 'Babelgum' },   { name: 'Powerset' },   { name: 'OpenX Enterprise' },   { name: 'OpenX Market' },   { name: 'OpenX Lift' },   { name: 'Sparter' },   { name: 'Thoof' },   { name: 'MyFree411' },   { name: 'Wesabe' },   { name: 'Prosper' },   { name: 'Stickam' },   { name: 'PayPerLive.com' } ] 
다단계 쿼리
다음 쿼리 text 연산자 사용하여 이름이 Facebook 인 회사 의 name 필드 검색하여 다음에 있는 모든 투자자(investments.person 및 investments.financial_org)가 투자한 금액(funding_rounds.raised_amount 및 funding_rounds.raised_currency_code)을 조회 합니다. 회사.
1 db.companies.aggregate([ 2   { 3     "$search": {  4       "returnStoredSource": true, 5       "returnScope": { "path": "funding_rounds" }, 6       "hasRoot": { 7         "operator": { 8           "text": { 9             "path": "name", 10             "query": "Facebook" 11           } 12         } 13       } 14     } 15   } 16 ]) 
[   {     raised_amount: 500000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: null,         person: {           first_name: 'Peter',           last_name: 'Thiel',           permalink: 'peter-thiel'         }       },       {         company: null,         financial_org: null,         person: {           first_name: 'Reid',           last_name: 'Hoffman',           permalink: 'reid-hoffman'         }       }     ]   },   {     raised_amount: 12700000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: { name: 'Accel Partners', permalink: 'accel-partners' },         person: null       },       {         company: null,         financial_org: null,         person: {           first_name: 'Mark',           last_name: 'Pincus',           permalink: 'mark-pincus'         }       },       {         company: null,         financial_org: null,         person: {           first_name: 'Reid',           last_name: 'Hoffman',           permalink: 'reid-hoffman'         }       }     ]   },   {     raised_amount: 27500000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: { name: 'Greylock Partners', permalink: 'greylock' },         person: null       },       {         company: null,         financial_org: {           name: 'Meritech Capital Partners',           permalink: 'meritech-capital-partners'         },         person: null       },       {         company: null,         financial_org: { name: 'Founders Fund', permalink: 'founders-fund' },         person: null       },       {         company: null,         financial_org: { name: 'SV Angel', permalink: 'sv-angel' },         person: null       }     ]   },   {     raised_amount: 240000000,     raised_currency_code: 'USD',     investments: [       {         company: { name: 'Microsoft', permalink: 'microsoft' },         financial_org: null,         person: null       }     ]   },   {     raised_amount: 60000000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: null,         person: {           first_name: 'Li',           last_name: 'Ka-shing',           permalink: 'li-ka-shing'         }       },       {         company: null,         financial_org: { name: 'Horizons Ventures', permalink: 'horizons-ventures' },         person: null       }     ]   },   {     raised_amount: 15000000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: {           name: 'European Founders Fund',           permalink: 'european-founders-fund'         },         person: null       }     ]   },   {     raised_amount: 100000000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: {           name: 'TriplePoint Capital',           permalink: 'triplepoint-capital'         },         person: null       }     ]   },   {     raised_amount: 60000000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: null,         person: {           first_name: 'Li',           last_name: 'Ka-shing',           permalink: 'li-ka-shing'         }       },       {         company: null,         financial_org: { name: 'Horizons Ventures', permalink: 'horizons-ventures' },         person: null       }     ]   },   {     raised_amount: 200000000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: {           name: 'Digital Sky Technologies',           permalink: 'digital-sky-technologies-fo'         },         person: null       }     ]   },   {     raised_amount: 210000000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: { name: 'Elevation Partners', permalink: 'elevation-partners' },         person: null       }     ]   },   {     raised_amount: 1500000000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: { name: 'Goldman Sachs', permalink: 'goldman-sachs' },         person: null       },       {         company: null,         financial_org: {           name: 'Digital Sky Technologies',           permalink: 'digital-sky-technologies-fo'         },         person: null       }     ]   } ] 
복합 쿼리
다음 쿼리 funding_rounds.investments 텀 사용하여 Ventures 설명된 name 회사에 투자한 텀 사용하는 금융 조직( 필드 내 모든 network 곳)( 필드 내 모든 description 곳)에 대해 중첩된 필드 검색합니다. ). 이 쿼리 복합 연산자 사용하여 다음을 검색  .
- embeddedDocument연산자 사용하는 중첩된- funding_rounds.investments.financial_org.name필드
- hasRoot연산자 사용하는 루트 수준- description필드
returnScope 은 연산자에 대한 컨텍스트를 funding_rounds embeddedDocuments 유형 필드 로 설정하고 returnStoredSource 필드 true 로 설정하다 storedSource 필드만 반환합니다. 이 쿼리 결과를 5 개의 문서로 제한합니다.
1 db.companies.aggregate([ 2   { 3     "$search": {  4       "compound": { 5         "should": [ 6           { 7             "embeddedDocument": { 8               "path": "funding_rounds.investments", 9               "operator": { 10                 "wildcard": { 11                   "path": "funding_rounds.investments.financial_org.name", 12                   "query": "*Ventures*", 13                   "allowAnalyzedField": true 14                 } 15               } 16             } 17           }, 18           { 19             "hasRoot": { 20               "operator": { 21                 "wildcard": { 22                   "path": "description", 23                   "query": "*network*", 24                   "allowAnalyzedField": true 25                 } 26               } 27             } 28           } 29         ] 30       }, 31       "returnScope": {"path": "funding_rounds"}, 32       "returnStoredSource": true 33     } 34   }, 35   { 36     "$limit": 5 37   } 38 ]) 
[   {     raised_amount: 6500000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: {           name: 'Velocity Interactive Group',           permalink: 'velocity-interactive-group'         },         person: null       },       {         company: null,         financial_org: { name: 'TELUS Ventures', permalink: 'telus-ventures' },         person: null       },       {         company: null,         financial_org: {           name: 'Charles River Ventures',           permalink: 'charles-river-ventures'         },         person: null       },       {         company: null,         financial_org: {           name: 'Partech Ventures',           permalink: 'partech-international'         },         person: null       },       {         company: null,         financial_org: { name: 'Fidelity Ventures', permalink: 'fidelity-ventures' },         person: null       },       {         company: null,         financial_org: { name: 'Star Ventures', permalink: 'star-ventures' },         person: null       },       {         company: null,         financial_org: { name: 'Castile Ventures', permalink: 'castile-ventures' },         person: null       }     ]   },   {     raised_amount: 25000000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: { name: 'Boulder Ventures', permalink: 'boulder-ventures' },         person: null       },       {         company: null,         financial_org: { name: 'Sequel Venture Partners', permalink: 'sequel' },         person: null       },       {         company: null,         financial_org: { name: 'Sprout Group', permalink: 'the-sprout-group' },         person: null       },       {         company: null,         financial_org: { name: 'EPIC Ventures', permalink: 'epic-ventures' },         person: null       },       {         company: null,         financial_org: {           name: 'Garage Technology Ventures',           permalink: 'garage-technology-ventures'         },         person: null       },       {         company: null,         financial_org: {           name: 'Pritzker Group Venture Capital',           permalink: 'pritzker-group-venture-capital'         },         person: null       },       {         company: null,         financial_org: { name: 'Ironside Ventures', permalink: 'ironside-ventures' },         person: null       },       {         company: null,         financial_org: { name: 'Valhalla Partners', permalink: 'valhalla-partners' },         person: null       },       {         company: null,         financial_org: { name: 'Vista Ventures', permalink: 'vista-ventures' },         person: null       },       {         company: null,         financial_org: { name: 'JPMorgan Chase & Co', permalink: 'jpmorgan-chase-co' },         person: null       },       {         company: null,         financial_org: {           name: 'DFJ Portage Ventures',           permalink: 'dfj-portage-ventures'         },         person: null       }     ]   },   {     raised_amount: 49500000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: { name: 'Versant Ventures', permalink: 'versant-ventures' },         person: null       },       {         company: null,         financial_org: { name: 'Skyline Ventures', permalink: 'skyline-ventures' },         person: null       },       {         company: null,         financial_org: {           name: 'Three Arch Partners',           permalink: 'three-arch-partners'         },         person: null       },       {         company: null,         financial_org: {           name: 'Affinity Capital Management',           permalink: 'affinity-capital-management'         },         person: null       },       {         company: null,         financial_org: { name: 'Alloy Ventures', permalink: 'alloy-ventures' },         person: null       },       {         company: null,         financial_org: { name: 'Delphi Ventures', permalink: 'delphi-ventures' },         person: null       },       {         company: null,         financial_org: { name: 'GBS Ventures', permalink: 'gbs-ventures' },         person: null       },       {         company: null,         financial_org: {           name: 'INVESCO Private Capital',           permalink: 'invesco-private-capital'         },         person: null       },       {         company: null,         financial_org: {           name: 'J.P. Morgan Securities Inc.',           permalink: 'j-p-morgan-securities-inc'         },         person: null       },       {         company: null,         financial_org: { name: 'ONSET Ventures', permalink: 'onset-ventures' },         person: null       }     ]   },   {     raised_amount: 1400000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: {           name: 'Accelerator Ventures',           permalink: 'accelerator-ventures'         },         person: null       },       {         company: null,         financial_org: { name: 'JAIC America', permalink: 'jaic-america' },         person: null       },       {         company: null,         financial_org: {           name: 'Pacific I&T Ventures',           permalink: 'pacific-i-t-ventures'         },         person: null       },       {         company: null,         financial_org: null,         person: {           first_name: 'Eric',           last_name: 'Di Benedetto',           permalink: 'eric-di-benedetto'         }       },       {         company: null,         financial_org: {           name: 'iSteps App Ventures',           permalink: 'isteps-app-ventures'         },         person: null       },       {         company: null,         financial_org: null,         person: {           first_name: 'Ben T.',           last_name: 'Smith, IV',           permalink: 'ben-smith-iv'         }       },       {         company: null,         financial_org: null,         person: {           first_name: 'Tim',           last_name: 'Stevens',           permalink: 'tim-stevens'         }       },       {         company: null,         financial_org: null,         person: {           first_name: 'Fabrice',           last_name: 'Grinda',           permalink: 'fabrice-grinda'         }       },       {         company: null,         financial_org: { name: 'LaunchCapital', permalink: 'launchcapital' },         person: null       }     ]   },   {     raised_amount: 1000000,     raised_currency_code: 'USD',     investments: [       {         company: null,         financial_org: {           name: 'DFJ Gotham Ventures',           permalink: 'dfj-gotham-ventures'         },         person: null       },       {         company: null,         financial_org: {           name: 'Metamorphic Ventures',           permalink: 'metamorphic-ventures-llc'         },         person: null       },       {         company: null,         financial_org: { name: 'RRE Ventures', permalink: 'rre-ventures' },         person: null       },       {         company: null,         financial_org: { name: 'Pilot Group', permalink: 'pilot-group' },         person: null       },       {         company: null,         financial_org: { name: 'AOL Ventures', permalink: 'aol-ventures' },         person: null       },       {         company: null,         financial_org: { name: 'Lerer Ventures', permalink: 'lerer-ventures' },         person: null       },       {         company: null,         financial_org: { name: 'Bowery Capital', permalink: 'bowery-capital' },         person: null       }     ]   } ]