hasRoot Operator

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

hasRoot Operator

Definition

The hasRoot operator can be used to query root-level fields when you specify the returnScope and returnStoredSource options.

Syntax

hasRoot has the following syntax:

{
  $search: {
    "index": "<index name>", // optional, defaults to "default"
    "hasRoot": {
      "operator": {
        <operator-specification>
      }
    },
    "returnScope": {
      "path": "<embedded-documents-field-to-retrieve>"
    },
    "returnStoredSource": true,
  }
}

Fields

Field Type Description Necessity
operator Object Operator to use to query an indexed field. This specifies the search criteria to filter the child documents. Required

Requirements

To use this operator, you must do the following in the index definition:

  • Index the root-level field that you want to query.

  • Index the array of objects that you want to retrieve as the embeddedDocuments type. You must also configure storedSource for nested fields that you want to retrieve.

In the query, you must do the following:

  • Specify returnScope to set the scope of the field to search for the operator.

  • Set returnStoredSource to true to return the storedSource fields.

Examples

The examples in this section use the sample_training.companies namespace. 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 products, 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.raised_currency_code

  • funding_rounds.raised_amount

  • funding_rounds.investments

  • products.name

{
  "mappings": {
    "dynamic": true,
    "fields": {
      "funding_rounds": {
        "dynamic": true,
        "fields": {
          "investments": [
            {
              "dynamic": true,
              "type": "embeddedDocuments"
            }
          ]
        },
        "storedSource": {
          "include": [
            "raised_currency_code",
            "raised_amount",
            "investments"
          ]
        },
        "type": "embeddedDocuments"
      },
      "products": {
        "dynamic": true,
        "storedSource": {
          "include": [
            "name"
          ]
        },
        "type": "embeddedDocuments"
      }
    }
  }
}

Simple Query

The following query returns products where the root-level field founded_year is in between the years 2005 and 2010.

db.companies.aggregate([
  {
    "$search": {
      "returnStoredSource": true,
      "returnScope": { "path": "products" },
      "hasRoot": {
        "operator": {
          "range": {
            "path": "founded_year",
            "gte": 2005,
            "lte": 2010
          }
        }
      }
    }
  }
])

Output:

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

Multi-Level Query

The following query searches the name field for the company named Facebook using the text operator to retrieve the amount invested (funding_rounds.raised_amount and funding_rounds.raised_currency_code) by all the investors (investments.person and investments.financial_org) in the company.

db.companies.aggregate([
  {
    "$search": {
      "returnStoredSource": true,
      "returnScope": { "path": "funding_rounds" },
      "hasRoot": {
        "operator": {
          "text": {
            "path": "name",
            "query": "Facebook"
          }
        }
      }
    }
  }
])

Output:

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

Compound Query

The following query searches the nested funding_rounds.investments field for financial organizations with the term Ventures (anywhere in the name field) that have invested in companies that are described using the term network (anywhere in the description field). The query uses the [compound](https://www.mongodb.com/docs/search/query/operators-collectors/compound.md#std-label-compound-ref operator to search the:

  • Nested funding_rounds.investments.financial_org.name field using the embeddedDocument operator

  • Root-level description field using the hasRoot operator

The returnScope sets the context for the operators to the funding_rounds embeddedDocuments type field and the returnStoredSource field is set to true to return only the storedSource fields. The query limits the results to 5 documents.

db.companies.aggregate([
  {
    "$search": {
      "compound": {
        "should": [
          {
            "embeddedDocument": {
              "path": "funding_rounds.investments",
              "operator": {
                "wildcard": {
                  "path": "funding_rounds.investments.financial_org.name",
                  "query": "*Ventures*",
                  "allowAnalyzedField": true
                }
              }
            }
          },
          {
            "hasRoot": {
              "operator": {
                "wildcard": {
                  "path": "description",
                  "query": "*network*",
                  "allowAnalyzedField": true
                }
              }
            }
          }
        ]
      },
      "returnScope": {"path": "funding_rounds"},
      "returnStoredSource": true
    }
  },
  {
    "$limit": 5
  }
])

Output:

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