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

hasRoot Operator

hasRoot

当指定 returnScopereturnStoredSource 选项时,hasRoot 操作符可用于查询根级字段。

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

operator

对象

用于查询索引字段的操作符。这指定了用于过滤子文档的搜索条件。

必需

要使用此操作符,您必须在索引定义中执行以下操作:

  • 索引您要查询的根级字段。

  • 将要检索的对象数组索引为 embeddedDocuments 类型。您还必须为要检索的嵌套字段配置storedSource

在查询中,您必须执行以下操作:

  • 指定 returnScope 以设置搜索操作符的字段范围。

  • returnStoredSource 设置为 true 以返回 storedSource 字段。

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

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

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

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

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

    • funding_rounds.raised_currency_code

    • funding_rounds.raised_amount

    • funding_rounds.investments

    • products.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_year20052010 年之间。

1db.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.personinvestments.financial_org)的投资金额(funding_rounds.raised_amountfunding_rounds.raised_currency_code)。

1db.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 个文档。

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