定义
语法
hasRoot 通过以下语法实现:
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 }
字段
字段 | 类型 | 说明 | 必要性 |
|---|---|---|---|
| 对象 | 用于查询索引字段的操作符。这指定了用于过滤子文档的搜索条件。 | 必需 |
要求
要使用此操作符,您必须在索引定义中执行以下操作:
索引您要查询的根级字段。
将要检索的对象数组索引为
embeddedDocuments类型。您还必须为要检索的嵌套字段配置storedSource。
在查询中,您必须执行以下操作:
指定
returnScope以设置搜索操作符的字段范围。将
returnStoredSource设置为true以返回storedSource字段。
示例
本节中的示例使用 sample_training.companies 命名空间。如果您加载了示例数据集并在集合上创建索引,则可以尝试本节中演示的示例查询。
样本索引
本节中的查询使用以下索引。此索引定义将 MongoDB Search 配置为执行以下操作:
自动索引集合中所有可动态索引的字段。
将
products、funding_rounds和funding_rounds.investments字段索引为 embeddedDocuments 类型。将以下字段存储在
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 }
简单查询
以下查询返回 products,其中根级字段 founded_year 在 2005 和 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 ])
多级查询
以下查询使用 text 操作符在 name 字段中搜索名为 Facebook 的公司,以检索公司所有投资者(investments.person和investments.financial_org)的投资金额(funding_rounds.raised_amount和funding_rounds.raised_currency_code)。
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 ])
复合查询
以下查询在嵌套的 funding_rounds.investments 字段中搜索包含术语 Ventures(在 name 字段中的任何位置)的金融组织,这些组织已投资于使用术语 network(在 description 字段中的任何位置)描述的公司。该查询使用复合操作符来搜索:
使用
embeddedDocument操作符的嵌套funding_rounds.investments.financial_org.name字段使用
hasRoot操作符的根级description字段
returnScope 为操作符设置上下文到 funding_rounds embeddedDocuments 类型字段,并将 returnStoredSource 字段设置为 true 以仅返回 storedSource 字段。该查询将结果限制为 5 个文档。
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 ])