Definition
hasAncestorThe
hasAncestoroperator queries anembeddedDocumentstype field that you specify in theancestorPath. TheancestorPathis a parent of the field that you specify in thereturnScope. ThereturnScopespecifies the nested field, including the level of nesting, that you want to retrieve.You can use this operator when scoring and retrieving objects within arrays as independent documents to:
Filter on ancestor fields (higher level of nesting).
Filter on sibling fields (equal level of nesting).
Syntax
The hasAncestor operator has the following syntax:
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 }
Fields
Field | Type | Description | Necessity |
|---|---|---|---|
| String | Name of the | Required |
| Object | Operator to use to query the nested field inside the
You can't use the | Required |
Requirements
To use this operator, you must:
Index the ancestor fields as the embeddedDocuments type. In the query, specify
ancestorPathas this field. If the field is on the root level, use hasRoot.Specify returnScope to set the scope of the document to query and retrieve.
Examples
The examples in this section use the sample_training.companies
dataset. If you load the sample dataset and create the index on the
collection, you can try the sample queries demonstrated in this section.
Sample Index
The queries in this section use the following index. This index definition configures MongoDB Search to do the following:
Automatically index all the dynamically indexable fields in the collection.
Index the
funding_roundsandfunding_rounds.investmentsfields as the embeddedDocuments type.Store the following fields on
mongot:funding_rounds.investments.financial_orgfunding_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 }
Sample Query
The following query searches the funding_rounds.founded_year field
for companies that were founded in the year 2005 using the
equals operator to retrieve investment details such as the financial
organizations or individuals that invested in the companies.
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 } ]