Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs Menu

Define Field Mappings

When you create a MongoDB Search index, you can specify which fields to index using the following methods:

  • Dynamic mappings: Enable MongoDB Search to automatically index all fields based on a default or configured set of field types (typeSet).

  • Static mappings: Enable you to specify which fields to index.

  • By default, MongoDB Search stops replicating changes for indexes larger than 2.1 billion index objects on a replica set or single shard, where each indexed document or nested embeddedDocument counts as a single object. This means that your index remains queryable, but you might get stale results.

    If you plan to index fields that might exceed 2.1 billion 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.

  • You can't index fields that contain the dollar ($) sign at the start of the field name.

  • 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 MongoDB Search Index Performance.

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}

You can configure MongoDB Search with:

  • Dynamic mappings to automatically index fields based on a default or configured set of types (typeSet)

  • Static mappings to only index specified fields

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

MongoDB Search dynamic mappings allow you to configure MongoDB Search to automatically and recursively index fields in your data. Fields can be indexed based on the default set of types or by configuring a typeSet.

You can enable or configure dynamic mappings:

  • At the root mappings level to apply to the entire document.

  • [Recommended] Within a document field type to apply to a specified object.

  • [Recommended] Within an embeddedDocuments field type to apply to a specified array of objects that require element-wise query comparisons (similar to $elemMatch).

Note

Best Practices

Dynamic mappings might result in indexing a high number of unique fields, which will occupy more disk space and might be less performant. Only use dynamic mappings if you need to index fields that change regularly or that are unknown. Always use dynamic mappings within a document, not at the parent document level.

When you use dynamic mappings to index your data:

  • MongoDB Search also dynamically indexes all nested fields supported by the typeSet in a document object in your data.

  • If a field contains polymorphic data, MongoDB Search automatically indexes the field as all of the types supported by the typeSet used in the index. If a field contains data of a type not supported by the typeSet, MongoDB Search won't index that data.

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 Type
MongoDB Search Field Type

Boolean

Date

Double, 32-bit Integer, 64-Bit Integer

ObjectId

String

UUID

Null

The following is the syntax for enabling default type set for dynamic mappings:

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

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

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.

A typeSet has the following syntax:

Note

You can't configure a typeSet from the Atlas UI Visual Editor. Use the Atlas UI JSON Editor instead.

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}

Before configuring a typeSet, consider the following:

  • You cannot define the same field type multiple times in the same typeSet object. You can configure only one typeSet definition for each field type.

    For example, you can't define multiple configurations for the number type in the same typeSet definition.

  • You can configure dynamic mappings with the multi analyzer.

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

Use static mappings to configure index options for fields that you don't want indexed dynamically, or to configure a single field independently from others in an index. When you use static mappings, MongoDB Search indexes only the fields that you specify in mappings.fields. You can also use static mappings to exclude fields from being indexed.

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.

If you omit the mappings.dynamic field, it defaults to 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.

The following example shows the field definition for indexing a field as multiple 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 doesn't support the following BSON data types:

  • Decimal128

  • JavaScript code with scope

  • Max key

  • Min key

  • Regular Expression

  • Timestamp

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.

These operators do not support an array of strings.

MongoDB Search doesn't include a field type for indexing null values because MongoDB Search automatically indexes null values for both statically and dynamically indexed fields.

Deprecated. To learn more about the deprecated facet field types and their updated counterparts, see Comparing Field Types for Facet.

Array of double or int values.

Note

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.

The following sample index definitions demonstrate how to use dynamic mappings.

Enable dynamic mappings to automatically index string field types.

The following index definition:

  • Enables dynamic mappings at the root level to automatically index fields in the collection based on the typeSet definition named only_strings, which indexes string fields in the collection with the positions index option and with store set to false. These configuration options save disk space by not storing the field values or term offsets for query highlighting.
{
"mappings": {
"dynamic": {
"typeSet": "only_strings"
}
},
"typeSets": [
{
"name": "only_strings",
"types": [
{
"type": "string",
"store": false,
"indexOptions": "positions"
}
]
}
]
}

Enable dynamic mappings within a document-type field to automatically index nested fields.

The following index definition:

  • Enables dynamic mappings at the root level to dynamically index fields in the collection based on the typeSet named only_strings, which indexes string fields in the collection with the positions index option and with store set to false, which saves disk space by not storing the field values or term offsets for query highlighting.

  • Indexes the awards field as the document type and uses the dynamic option with the only_numbers typeSet to dynamically index only the numeric sub-fields in the awards document. This means that the numeric sub-fields wins and nominations are indexed, but the string sub-field text is not indexed.

{
"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"
}
]
}
]
}

Dynamically map specific field types except for specified fields.

The following index definition:

  • Configures dynamic mappings for specific field types using the typeSet named indexedTypes, which specifies the following behavior:

    • Automatically index string fields as the token type.

    • Automatically index numeric fields as the number type.

  • Excludes the plot field from being indexed.

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

The following sample index definitions demonstrate how to use static mappings.

Disable dynamic mappings and define individual fields for indexing.

The following index definition:

  • Specifies static field mappings at the root level (dynamic: false), which means that fields that are not specified in mappings.fields are not indexed. The index definition explicitly includes the following fields for indexing:

    • The awards field, which is of type document. It has three embedded sub-fields, wins, nominations and text.

      The wins and nominations sub-fields contain numeric data and are indexed as the type number. nominations is indexed with the int64 representation, which allows it to support larger integer values than the default number type.

      The text sub-field contains string data and is indexed as the string type using the lucene.english analyzer, which is optimized for English text. The text field also uses the ignoreAbove option to ignore any string of more than 255 characters in length.

    • The title field, which contains string data and is indexed as type string using the lucene.whitespace analyzer, which is optimized for text that contains a lot of whitespace, such as product names or titles. The title field uses the multi option to specify the lucene.french analyzer as a secondary analyzer. This allows you to specify { "value": "title", "multi": "frenchAnalyzer" } as the path in your $search queries to better target French query terms.

    • The genres field, which contains an array of strings and is indexed as the type string. MongoDB Search indexes the array elements using the lucene.standard analyzer by default. For indexing arrays, MongoDB Search only requires the data type of the array elements. You don't have to specify that the data is contained in an array in the index definition.

{
"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"
}
}
}
}

The following index definition examples combine dynamic mappings with static mappings.

Disable dynamic mappings at the root level, but define static mappings and enable dynamic mappings for nested fields.

This index definition:

  • Specifies static field mappings (dynamic: false), which means fields and field types that aren't explicitly mentioned aren't indexed. So, the index definition includes:

    • The title field, which contains string data and is indexed as the string type using the lucene.whitespace analyzer, which is optimized for text that contains a lot of whitespace, such as product names or titles.

    • The awards field, which is of type document and contains three sub-fields. The wins and nominations sub-fields contain numeric data, and the text sub-field contains string data. Instead of explicitly setting the data type for each nested field in the document, the index definition enables dynamic mapping for all the sub-fields in the document. This indexes wins and nominations as the number type, and text as the string type. Data in the text field is indexed using the lucene.standard analyzer by default.

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

Set static mappings at the root level, and dynamically map a nested field using a custom typeSet definition.

This index definition:

  • Specifies static field mappings at the root level (dynamic: false), which means that fields that aren't specified in the mappings.fields option aren't indexed.

  • Defines dynamic mappings for the awards field, which is of type document, using the custom typeSet named movieAwards. The awards field has three embedded sub-fields: wins, nominations, and text. Instead of automatically indexing the sub-fields by setting dynamic to true at the root level or by explicitly indexing each nested field using static mappings, the index definition configures dynamic mappings for the document field type using the typeSet definition named movieAwards. The movieAwards typeSet does the following:

    • Indexes string fields as the string type using the multi option to specify multiple analyzers: lucene.english and lucene.french.

    • Indexes numeric fields as type number using the default settings for the number type.

      {
      "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"
      }
      ]
      }
      ]
      }