Docs 菜单
Docs 主页
/ /

hasAncestor Operator

hasAncestor

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

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

  • 过滤祖先字段(更高级别的嵌套)。

  • 对同级字段进行筛选(同等嵌套级别)。

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

ancestorPath

字符串

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

必需

operator

对象

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

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

必需

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

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

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

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

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

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

  • funding_roundsfunding_rounds.investments 字段索引为 embeddedDocuments 类型。

  • 将以下字段存储在 mongot 上:

    • funding_rounds.investments.financial_org

    • funding_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 年的公司,以检索投资详细信息,例如投资这些公司的金融组织或个人。

1db.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 }
]

后退

geoWithin

在此页面上