对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs 菜单

定义字段映射

创建MongoDB 搜索索引时,可以使用以下方法指定要索引的字段:

  • 动态映射:启用 MongoDB 搜索根据默认或配置的字段类型 (typeSet) 自动为所有字段建立索引。

  • 静态映射:允许您指定要索引的字段。

  • 默认下, MongoDB Search 停止复制副本副本集或单个分片上超过 2.1 十亿个索引对象的索引更改,其中每个已建立索引的文档或嵌套 embeddedDocument 都算作单个对象。这意味着您的索引仍然可查询,但可能会得到过时的结果。

    如果计划对可能超过 2.1 亿个对象的字段进行索引,其中索引对象是顶层文档或嵌入式文档,请使用 numPartitions 索引选项对索引进行分区(仅在搜索节点部署上支持)或 分片集群。

  • 您无法对字段名称开头包含美元 ($) 符号的字段创建索引。

  • 与其他字段类型相比,自动完成字段类型可以创建大型索引,且构建时间更长。虽然您可以通过将 autocomplete 类型包含在自定义 typeSet 定义中来在动态映射中使用该类型,但我们建议仅在静态映射中使用 autocomplete 类型,以避免对性能、存储和评分产生意外影响。要学习;了解更多信息,请参阅如何为自动完成索引字段MongoDB搜索索引性能。

The following syntax demonstrates how to enable MongoDB Search to index fields using dynamic and static mappings. To learn more about dynamic and static mappings, see Dynamic and Static Mappings.

1{
2 "mappings": {
3 "dynamic": true|false | {
4 "typeSet": "<typeset-name>"
5 },
6 "fields": {
7 "<field-name>": {
8 "type": "<field-type>",
9 ...
10 },
11 ...
12 }
13 },
14 "typeSets": [
15 {
16 "name": "<typeset-name>",
17 "types": [
18 {
19 "type": "<field-type>",
20 ...
21 },
22 ...
23 ]
24 },
25 ...
26 ]
27}

您可以通过以下方式配置MongoDB搜索:

  • 动态映射,用于根据默认或配置的类型设立(typeSet) 自动为字段索引

  • 仅索引指定字段的静态映射

You can also use dynamic mappings with static mappings. Static mappings override the dynamic mappings configuration.

MongoDB 搜索动态映射允许您将MongoDB 搜索配置为自动递归地索引数据中的字段。可以根据默认类型设立或通过配置 typeSet 对字段进行索引。

您可以启用或配置动态映射:

  • 在根 mappings 级别应用整个文档。

  • [推荐]document字段中键入要应用指定对象的字段。

  • [推荐]embeddedDocuments 字段类型中,要应用需要逐元素查询比较的指定对象数组(类似于 $elemMatch)。

注意

最佳实践

动态映射可能会导致对大量唯一字段索引,从而占用更多磁盘空间并且可能降低性能。仅当需要对定期更改或未知的字段索引时,才使用动态映射。始终在文档中而不是在父文档级别使用动态映射。

当您使用动态映射对数据进行索引时:

  • MongoDB 搜索还会对数据中 document 对象中的 typeSet 支持的所有嵌套字段进行动态索引。

  • 如果字段包含多态数据, MongoDB Search 会自动将该字段索引为索引中使用的 typeSet 支持的所有类型。如果字段包含 typeSet 不支持类型的数据, MongoDB 搜索 将不会索引该数据。

dynamic 设置为 true 时,MongoDB Search 使用默认的 typeSet。在默认的 typeSet 中,MongoDB Search 将 BSON 类型索引为 MongoDB Search 字段类型。下表显示使用默认 typeSet 时,MongoDB Search 自动索引为 MongoDB Search 字段类型的 BSON 类型。MongoDB Search 还会自动索引包含在数组和对象中的以下 BSON 类型。

BSON类型
MongoDB搜索字段类型

布尔

Date

双精度浮点数、32 位整数、64 位整数

ObjectId

字符串

UUID

null

以下是为动态映射启用默认类型设立的语法:

1{
2 "mappings": {
3 "dynamic": true
4 }
5}

For index examples that demonstrate indexing all fields using the default typeSet, see Dynamic Mapping Examples.

通过配置 typeSet,指定要动态索引的 MongoDB 搜索字段类型。您可以将任何 BSON 类型配置为任何 MongoDB Search 字段类型自动编制索引,但不包括 documentembeddedDocumentsvector 或已弃用的字段类型。

typeSet 采用以下语法:

注意

您无法从 Atlas 用户界面 Visual Editor 中配置 typeSet。请改用 Atlas 用户界面 JSON Editor

1{
2 "mappings": {
3 "dynamic": {
4 "typeSet": "<typeset-name>"
5 },
6 "fields": {
7 "<field-name>": {
8 "type": "<field-type>",
9 ...
10 },
11 ...
12 }
13 },
14 "typeSets": [
15 {
16 "name": "<typeset-name>",
17 "types": [
18 {
19 "type": "<field-type>"
20 },
21 ...
22 ]
23 },
24 ...
25 ]
26}

在配置 typeSet 之前,请考虑以下事项:

  • 不能在同一 typeSet对象中多次定义相同的字段类型。您只能为每种字段类型配置一个 typeSet 定义。

    示例,您不能在同一 typeSet 定义中为 number 类型定义多个配置。

  • 您可以使用 multi分析器配置动态映射。

For index examples that demonstrate using custom typeSet configuration, see Dynamic Mapping Examples.

使用静态映射为您希望动态编制索引的字段配置索引选项,或者独立于索引中的其他字段配置单个字段。使用静态映射时, MongoDB Search 仅对您在 mappings.fields 中指定的字段编制索引。您还可以使用静态映射将字段排除在索引之外。

To use static mappings to configure index options for only some fields, set mappings.dynamic to false and specify the field name, data type, and other configuration options for each field that you want to index. You can specify the fields in any order.

如果您忽略 mappings.dynamic 字段,则默认为 false

1{
2 "mappings": {
3 "dynamic": true|false,
4 "fields": {
5 "<field-name>": {
6 "type": "<field-type>",
7 ...
8 },
9 ...
10 }
11 }
12}

To define the index for a nested field, you must define the mappings for each parent field of that nested field. You can't use dot notation to statically index nested fields. For examples, see the Examples or Combined Mapping Example below.

You can use static mappings to index fields as multiple types. To index a field as multiple types, define the types in the field definition array for the field. You can index any field as any supported types using static mappings. To learn more, see MongoDB Search Field Types.

以下示例显示了将一个字段索引为多种类型的字段定义。

1{
2 "mappings": {
3 "dynamic": true|false | {
4 "typeSet": "<type-set-name>"
5 },
6 "fields": {
7 "<field-name>": [
8 {
9 "type": "<field-type>",
10 ...
11 },
12 {
13 "type": "<field-type>",
14 ...
15 },
16 ...
17 ],
18 ...
19 }
20 }.
21 "typeSets": [
22 {
23 "name": "<typeset-name>",
24 "types": [
25 {
26 "type": "<field-type>"
27 },
28 ...
29 ]
30 },
31 ...
32 ]
33}

For other index examples that demonstrate static mappings, see Static Mapping Example.

MongoDB Search 不支持以下BSON数据类型:

  • Decimal128

  • 带作用域的 JavaScript 代码

  • Max key

  • Min key

  • 正则表达式

  • 时间戳

MongoDB Search automatically stores fields of type string on mongot. You can store fields of all supported data types on MongoDB Search using the Define Stored Source Fields in Your MongoDB Search Index option in your index definition. To learn more about mongot and MongoDB Search node architecture, see MongoDB Search Deployment Options.

下表列举了支持的 BSON 数据类型 和可用于对 BSON 数据类型进行索引的 MongoDB Search 字段类型 。该表还列出了可用于查询字段值的 操作符和收集器

这些操作符不支持字符串数组。

MongoDB Search 不包含用于索引null 值的字段类型,因为MongoDB Search 会自动为静态和动态索引字段的 null 值索引。

已弃用。要了解有关已弃用的分面(Facet)字段类型及其更新的对应字段类型的更多信息,请参阅比较分面字段类型。

doubleint 值的数组。

注意

You can store fields of all supported data types on MongoDB Search using the storedSource option.

sample_mflix.movies 集合的以下示例索引定义展示了如何使用动态和静态映射对字段进行索引。您可以在集群上加载示例数据来尝试这些示例。要了解如何创建 MongoDB Search 索引,请参阅MongoDB Search 快速入门。

以下示例索引定义演示了如何使用动态映射。

启用动态映射以自动索引 string 字段类型。

以下索引定义:

  • 在根级别启用动态映射,以根据名为 only_stringstypeSet 定义自动为集合中的字段索引,该定义使用 positions索引选项并将 store设立为 false 来为集合中的 string 字段编制索引。这些配置选项不存储查询突出显示的字段值或术语偏移,从而节省磁盘空间。
{
"mappings": {
"dynamic": {
"typeSet": "only_strings"
}
},
"typeSets": [
{
"name": "only_strings",
"types": [
{
"type": "string",
"store": false,
"indexOptions": "positions"
}
]
}
]
}

在文档类型字段中启用动态映射以自动索引嵌套字段。

以下索引定义:

  • 在根级别启用动态映射,以根据名为 only_stringstypeSet 动态索引集合中的字段,这将使用 positions索引选项并将 store设立为 false 来为集合中的 string 字段编制索引,不存储查询突出显示的字段值或术语偏移量,从而节省磁盘空间。

  • awards字段索引为 document 类型,并使用带有 only_numbers typeSetdynamic 选项,仅动态索引awards文档中的数字子字段。这意味着数字子字段 winsnominations 已建立索引,但 string 子字段 text 未建立索引。

{
"mappings": {
"dynamic": {
"typeSet": "only_strings"
},
"fields": {
"awards": {
"type": "document",
"dynamic": {
"typeSet": "only_numbers"
}
}
}
},
"typeSets": [
{
"name": "only_numbers",
"types": [
{
"type": "number"
}
]
},
{
"name": "only_strings",
"types": [
{
"type": "string",
"store": false,
"indexOptions": "positions"
}
]
}
]
}

动态映射除指定字段之外的特定字段类型。

以下索引定义:

  • 使用名为 indexedTypestypeSet 为特定字段类型配置动态映射,其行为如下:

    • 自动将 string 字段索引为 token 类型。

    • 自动将数字字段索引为 number 类型。

  • 不将 plot字段纳入索引。

{
"mappings": {
"dynamic": {
"typeSet": "indexedTypes"
},
"fields": {
"plot": []
}
},
"typeSets": [
{
"name": "indexedTypes",
"types": [
{
"type": "token"
},
{
"type": "number"
}
]
}
]
}

以下示例索引定义演示了如何使用静态映射。

禁用动态映射并定义用于索引的单独字段。

以下索引定义:

  • 在根级别 (dynamic: false) 指定静态字段映射,这意味着未在 mappings.fields 中指定的字段不会被编入索引。索引定义明确包含以下用于索引的字段:

    • awards字段,其类型为 document。它具有三个嵌入式子字段:winsnominationstext

      winsnominations 子字段包含数值数据,并按 number 类型进行索引。nominations 使用 int64 表示形式进行索引,这使其能够支持比默认number 类型更大的整数值。

      text 子字段包含 string 数据,并使用针对英文文本优化的 lucene.english 分析器将其索引为 string 类型。text字段还使用 ignoreAbove 选项忽略任何长度超过 255 个字符的字符串。

    • title字段,包含字符串数据,并使用lucene.whitespace分析器将其索引为 string 类型,该分析器针对包含大量空格的文本(例如产品名称或标题)进行了优化。title字段使用multi选项将lucene.french分析器指定为从节点(secondary node from replica set)分析器。这样,您就可以在 { "value": "title", "multi": "frenchAnalyzer" } 查询中将 path 指定为 $search,从而更好地定位法语查询词语。

    • genres字段,包含一个字符串数组,并按 string 类型进行索引。默认下, MongoDB Search 使用 lucene.standard 分析器对数组元素进行索引。对于索引数组, MongoDB Search 仅需要数组元素的数据类型。您不必在索引定义中指定数据包含在数组中。

{
"mappings": {
"dynamic": false,
"fields": {
"awards": {
"type": "document",
"fields": {
"wins": {
"type": "number"
},
"nominations": {
"type": "number",
"representation": "int64"
},
"text": {
"type": "string",
"analyzer": "lucene.english",
"ignoreAbove": 255
}
}
},
"title": {
"type": "string",
"analyzer": "lucene.whitespace",
"multi": {
"frenchAnalyzer": {
"type": "string",
"analyzer": "lucene.french"
}
}
},
"genres": {
"type": "string"
}
}
}
}

以下索引定义示例将动态映射与静态映射相结合。

在根级别禁用动态映射,但定义静态映射并为嵌套字段启用动态映射。

此索引定义:

  • 指定静态字段映射 (dynamic: false),这意味着未显式提及的字段和字段类型不会被编入索引。因此,索引定义包括:

    • title字段,包含字符串数据,并使用lucene.whitespace分析器将其索引为 string 类型,该分析器针对包含大量空格的文本(例如产品名称或标题)进行了优化。

    • awards字段,其类型为 document,包含三个子字段。winsnominations 子字段包含数值数据,text 子字段包含 string 数据。索引定义无需为文档中的每个嵌套字段显式设置数据类型,而是可为文档中的所有子字段启用动态映射。这会将 winsnominations 索引为 number 类型,并将 text 索引为 string 类型。默认下,使用 lucene.standard 分析器对 text字段中的数据进行索引。

    {
    "mappings": {
    "dynamic": false,
    "fields": {
    "title": {
    "type": "string",
    "analyzer": "lucene.whitespace"
    },
    "awards": {
    "type": "document",
    "dynamic": true
    }
    }
    }
    }

在根级别设置静态映射,并使用自定义 typeSet 定义动态映射嵌套字段。

此索引定义:

  • 在根级别指定静态字段映射 (dynamic: false),这意味着未在 mappings.fields 选项中指定的字段不会被编制索引。

  • 使用名为 movieAwards 的自定义 typeSetawards字段(类型为 document)定义动态映射。awards字段具有三个嵌入式子字段:winsnominationstext。该索引定义使用 typeSetdocument字段类型配置动态索引,而不是通过在根级别将 dynamic 设置为 true 或使用静态映射显式索引每个嵌套字段来自动为子字段建立索引。名为 movieAwards 的定义。movieAwards typeSet 执行以下操作:

    • 使用 multi 选项将 string 字段索引为 string 类型,以指定多个分析器:lucene.englishlucene.french

    • 使用 number 类型的默认设置,将数字字段索引为 number 类型。

      {
      "mappings": {
      "dynamic": false,
      "fields": {
      "awards": {
      "type": "document",
      "dynamic": {
      "typeSet": "movieAwards"
      }
      }
      }
      },
      "typeSets": [
      {
      "name": "movieAwards",
      "types": [
      {
      "type": "string",
      "multi": {
      "english": {
      "type": "string",
      "analyzer": "lucene.english"
      },
      "french": {
      "type": "string",
      "analyzer": "lucene.french"
      }
      }
      },
      {
      "type": "number"
      }
      ]
      }
      ]
      }