Definition
Syntax
hasRoot has the following syntax:
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 }
Fields
Field | Type | Description | Necessity |
|---|---|---|---|
| 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
embeddedDocumentstype. You must also configurestoredSourcefor nested fields that you want to retrieve.
In the query, you must do the following:
Specify
returnScopeto set the scope of the field to search for the operator.Set
returnStoredSourcetotrueto return thestoredSourcefields.
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, andfunding_rounds.investmentsfields as the embeddedDocuments type.Store the following fields on
mongot:funding_rounds.raised_currency_codefunding_rounds.raised_amountfunding_rounds.investmentsproducts.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 }
Simple Query
The following query returns products where the root-level field founded_year is in between the years 2005 and 2010.
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 ])
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.
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 ])
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 operator to search the:
Nested
funding_rounds.investments.financial_org.namefield using theembeddedDocumentoperatorRoot-level
descriptionfield using thehasRootoperator
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.
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 ])