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

为 MongoDB Search 索引定义字段映射

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

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

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

  • 默认情况下,对于大于 2,100,000,000 个索引对象的索引,MongoDB Search 将停止在副本集或单个分片上复制对这些索引的更改,其中每个已建立索引的文档或嵌套 embeddedDocument 都算作一个对象。这意味着您的索引是可查询的,但您可能会得到过时的结果。

    If you plan to index fields that might exceed 2,100,000,000 objects, where an index object is a top-level document or an embedded document, use the numPartitions index option to partition your index (supported only on Search Nodes deployment) or shard your cluster.

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

  • The autocomplete field type can create large indexes and take longer to build than other field types. While you can use the autocomplete type in dynamic mappings by including it in a custom typeSet definition, we recommend using the autocomplete type in static mappings only to avoid unintended performance, storage, and scoring implications. To learn more, see How to Index Fields for Autocompletion and Improve MongoDB Search Index Performance.

以下语法演示了如何启用 MongoDB Search 以使用动态和静态映射来索引字段。要了解有关动态和静态映射的更多信息,请参阅动态和静态映射。

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. When you define a static mapping for a field, MongoDB Search indexes that field only according to the static mapping definition. The dynamic mapping no longer applies to that field.

例如,如果您启用 dynamic: true,MongoDB Search 默认会将 string 字段索引为 string 类型。如果您为特定 string 字段添加静态 autocomplete 映射,MongoDB Search 仅会将该字段索引为 autocomplete 类型。它也不会索引为 string,除非您在静态映射中明确包含 string。要将字段索引为这两种类型,必须在字段的静态映射数组中定义这两种类型。

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

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

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

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

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

注意

最佳实践

动态映射可能会导致索引大量独特字段,这会占用更多磁盘空间,并可能会降低性能。仅在需要索引定期变更或未知字段时使用动态映射。始终在文档内使用动态映射,而不是在父文档级别使用动态映射。

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

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

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

MongoDB Search uses the default typeSet when dynamic is set to true. In the default typeSet, MongoDB Search indexes the BSON types as MongoDB Search field types. The following table shows the BSON types that MongoDB Search automatically indexes as MongoDB Search field types when you use the default typeSet. MongoDB Search also automatically indexes the following BSON types when they are contained within arrays and objects.

BSON 类型
MongoDB搜索字段类型

布尔

Date

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

ObjectId

字符串

UUID

null

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

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

有关演示使用默认typeSet对所有字段进行索引的索引示例,请参阅动态映射示例。

Specify the MongoDB Search field types to index dynamically by configuring a typeSet. You can configure any BSON type to be automatically indexed as any MongoDB Search field type except document, embeddedDocuments, vector, or deprecated field types.

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分析器配置动态映射。

有关演示使用自定义 typeSet 配置的索引示例,请参阅动态映射示例。

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

要使用静态映射仅为某些字段配置索引选项,请将 mappings.dynamic 设为 false,并为要建立索引的每个字段指定字段名称、数据类型和其他配置选项。您可以按任意顺序指定字段。

如果您忽略 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}

要定义嵌套字段的索引,必须为该嵌套字段的每个父字段定义映射。不能使用点表示法对嵌套字段进行静态索引。有关示例,请参阅下面的示例组合映射示例

您可以使用静态映射将字段索引为多种类型。要将字段索引为多种类型,请在字段定义数组中为字段定义类型。您可以使用静态映射将任何字段索引为任何支持的类型。要学习;了解更多信息,请参阅MongoDB搜索字段类型。

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

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}

有关演示静态映射的其他索引示例,请参阅静态映射示例。

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.

The following table enumerates the supported BSON data types and the MongoDB Search field types that you can use to index the BSON data types. The table also lists the operators and collectors that you can use to query the field values.

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

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

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

doubleint 值的数组。

注意

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

The following sample index definitions on the sample_mflix.movies collection demonstrate how to index fields using dynamic and static mappings. You can load the sample data on your cluster to try the examples. To learn more about how to create MongoDB Search indexes, see MongoDB Search Quick Start.

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

启用动态映射以自动索引 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"
      }
      ]
      }
      ]
      }