Docs Menu
Docs Home
/ /

hasAncestor Operator

hasAncestor

The hasAncestor operator queries an embeddedDocuments type field that you specify in the ancestorPath. The ancestorPath is a parent of the field that you specify in the returnScope. The returnScope specifies 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).

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}
Field
Type
Description
Necessity

ancestorPath

String

Name of the embeddedDocuments type field that is an intermediary ancestor of the nested field used in the query. This is the parent document that the operator evaluates.

Required

operator

Object

Operator to use to query the nested field inside the embeddedDocuments type field specified in the ancestorPath. The query path must be a child of the ancestorPath, regardless of the level of nesting. This specifies the search criteria to filter the parent document.

You can't use the hasAncestor operator inside the operator specification.

Required

To use this operator, you must:

  • Index the ancestor fields as the embeddedDocuments type. In the query, specify ancestorPath as 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.

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.

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_rounds and funding_rounds.investments fields as the embeddedDocuments type.

  • Store the following fields on 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}

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.

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

Back

geoWithin

On this page