定义
语法
hasAncestor操作符的语法如下:
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 }
字段
字段 | 类型 | 说明 | 必要性 |
|---|---|---|---|
| 字符串 |
| 必需 |
| 对象 | 操作符,用于查询 您不能在操作符规范中使用 | 必需 |
要求
要使用此操作符,您必须:
将祖先字段索引为 embeddedDocuments
ancestorPath类型。在查询中,将 指定为此字段。如果该字段位于根级别,请使用 hasRoot。指定 returnScope 以设立要查询和检索的文档范围。
示例
本部分中的示例使用 sample_training.companies 数据集。如果您加载示例数据集并在集合上创建索引,则可以尝试本节中演示的示例查询。
样本索引
本节中的查询使用以下索引。此索引定义将MongoDB Search 配置为执行以下操作:
自动索引集合中所有可动态索引的字段。
将
funding_rounds和funding_rounds.investments字段索引为 embeddedDocuments 类型。将以下字段存储在
mongot上:funding_rounds.investments.financial_orgfunding_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 }
样本查询
以下查询使用 equals操作符在 funding_rounds.founded_year字段中搜索成立于 2005 年的公司,以检索投资详细信息,例如投资这些公司的金融组织或个人。
1 db.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 } ]