hasAncestor Operator

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

hasAncestor Operator

Definition

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).

Syntax

The hasAncestor operator has the following syntax:

{
  $search: {
    "index": "<index name>", // optional, defaults to "default"
    "hasAncestor": {
      "ancestorPath": "<embeddedDocuments-type-higher-level-field>",
      "operator": {
        <operator-specification>
      }
    },
    "returnScope": {
      "path": "<embeddedDocuments-type-field-to-retrieve>"
    }
  }
}

Fields

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

Requirements

To use this operator, you must:

  • Index the ancestor fields as the [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type. In the query, specify ancestorPath as this field. If the field is on the root level, use [hasRoot.](https://www.mongodb.com/docs/search/query/operators-collectors/hasroot.md#std-label-has-root-ref

  • Specify [returnScope](https://www.mongodb.com/docs/search/query/return-scope.md#std-label-fts-return-scope 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_rounds and funding_rounds.investments fields as the [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type.

  • Store the following fields on mongot:

  • funding_rounds.investments.financial_org

  • funding_rounds.investments.person

{
  "mappings": {
    "dynamic": true,
    "fields": {
      "funding_rounds": {
        "dynamic": true,
        "fields": {
          "investments": [
            {
              "dynamic": true,
              "storedSource": {
                "include": [
                  "financial_org",
                  "person"
                ]
              },
              "type": "embeddedDocuments"
            }
          ]
        },
        "type": "embeddedDocuments"
      }
    }
  }
}

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.

db.companies.aggregate([
  {
    "$search": {
      "returnStoredSource": true,
      "returnScope": { "path": "funding_rounds.investments" },
      "hasAncestor": {
        "ancestorPath": "funding_rounds",
        "operator": {
          "equals": {
            "path": "funding_rounds.funded_year",
            "value": 2005
          }
        }
      }
    }
  }
])

Output:

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