对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

hasAncestor Operator

hasAncestor

hasAncestor 操作符查询您在 ancestorPath 中指定的 embeddedDocuments 类型字段。ancestorPath 是您在 returnScope 中指定的字段的父级。returnScope 指定嵌套字段,包括您要检索的嵌套字段。

在对数组中的对象作为独立文档进行评分和检索时,您可以使用此操作符:

  • 按祖先字段进行过滤器(高级别嵌套)。

  • 对同级字段(相同级别嵌套)进行过滤器。

hasAncestor操作符的语法如下:

{ $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>" } }}
字段
类型
说明
必要性

ancestorPath

字符串

embeddedDocuments 类型字段的名称,它是查询中使用的嵌套字段的中间祖先。这是操作符评估的父文档。

必需

operator

对象

用于查询 ancestorPath 中指定的 embeddedDocuments 类型字段内的嵌套字段的操作符。查询路径必须是 ancestorPath 的子路径,无论嵌套层级如何。指定过滤父文档的搜索条件。

不能在操作符规范内使用 hasAncestor 操作符。

必需

要使用该操作符,您必须:

  • 将祖先字段索引为 embeddedDocuments 类型。在查询中,将 ancestorPath 指定为此字段。如果该字段位于根级别,请使用 hasRoot。

  • 指定 returnScope 以设置要查询和检索的文档范围。

本节中的示例使用sample_training.companies 数据集。如果您加载了示例数据集并在集合上创建索引,则可以尝试本部分中演示的示例查询。

本节中的查询使用以下索引。此索引定义将 MongoDB Search 配置为执行以下操作:

  • 自动索引集合中所有可动态索引的字段。

  • 将 funding_rounds 和 funding_rounds.investments 字段索引为 embeddedDocuments 类型。

  • 将以下字段存储在 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" } } }}

以下查询使用 equals 操作符在 funding_rounds.founded_year 字段中搜索成立于 2005 年的公司,以检索投资明细,例如投资于这些公司的金融组织或个人。

db.companies.aggregate([ { "$search": { "returnStoredSource": true, "returnScope": { "path": "funding_rounds.investments" }, "hasAncestor": { "ancestorPath": "funding_rounds", "operator": { "equals": { "path": "funding_rounds.funded_year", "value": 2005 } } } } }])
[ { 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 }]