Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/
Atlas
/ / /

Define Field Mappings

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

  • Dynamic mappings: Enables MongoDB Search to automatically index all fields of supported types.

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

  • If you create an MongoDB Search index that has or will soon have more than 2.1 billion index objects, you must use numPartitions or shard your cluster. For this limit, each top-level document or nested embeddedDocument in the indexed collection fields counts as one object

    By default, MongoDB Search stops replicating changes for a single index that grows larger than 2.1 billion index objects on any given replica set member or shard. This means 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 deployments) or shard your cluster.

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

To use dynamic mappings, set mappings.dynamic to true in your index definition. You can also configure individual fields by specifying the field name, data type, and other configuration options for each field that you want to configure in mappings.fields. You can specify the fields in any order.

1{
2 "mappings": {
3 "dynamic": true,
4 "fields": { // Optional, use this to configure individual fields
5 "<field-name>": {
6 "type": "<field-type>",
7 ...
8 },
9 ...
10 }
11 }
12}

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

If you omit the mappings.dynamic field, it defaults to false.

1{
2 "mappings": {
3 "dynamic": false, // Optional, if omitted defaults to "false"
4 "fields": {
5 "<field-name>": {
6 "type": "<field-type>",
7 ...
8 },
9 ...
10 }
11 }
12}

You can index a field that contains polymorphic data as multiple types by specifying an array of field definitions for the field, one for each data type.

Syntax
1{
2 "mappings": {
3 "dynamic": <boolean>,
4 "fields": {
5 "<field-name>": [
6 {
7 "type": "<field-type>",
8 ...
9 },
10 {
11 "type": "<field-type>",
12 ...
13 },
14 ...
15 ],
16 ...
17 },
18 ...
19 }
20}

You can use static and dynamic mappings to specify whether MongoDB Search must automatically index all the dynamically indexable fields in your collection.

Use dynamic mapping to automatically index all fields of supported types in your collection. We recommend using dynamic mappings only if your schema changes regularly or is unknown, or if you're experimenting with MongoDB Search. Dynamically mapped indexes occupy more disk space than statically mapped indexes and may be less performant.

When you dynamically index a field that contains polymorphic data, MongoDB Search automatically indexes the field as all of the dynamically indexable field types that correspond to the data. If the field contains data of a type that MongoDB Search doesn't index automatically, MongoDB Search won't index that data.

MongoDB Search dynamically indexes all fields in a document using the default settings for the detected data type. MongoDB Search also dynamically indexes all nested documents under the document, unless you explicitly override by setting dynamic to false.

To learn which data types support dynamic indexing, see the Data Types section below.

Use static mapping 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 mapping, MongoDB Search indexes only the fields that you specify in mappings.fields.

When you statically index a field containing polymorphic data, MongoDB Search only indexes documents that correspond to the mapping specified in the index definition for that field. MongoDB Search doesn't index documents that don't match the data type specified in the index definition for that field.

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 the dot notation to statically index nested fields. For examples, see the Examples or Combined Mapping Example below.

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 indicates whether the MongoDB Search field type is automatically included in an MongoDB Search index when you enable dynamic mappings and lists the operators and collectors that you can use to query the field value.

BSON Type
MongoDB Search Field Type
Dynamically Indexed
Operators and Collectors

✓

Operators that support the data type in the array.

Boolean

✓

Date

✓

Date

Double

✓

Double

Double

32-bit integer

✓

32-bit integer

64-bit integer

✓

64-bit integer

Null

N/A

✓

Object

✓

All Operators

Object

embeddedDocument (for array of objects)

ObjectId

✓

String

✓

String

String

String

✓

Some limitations apply. To learn more, see How to Index the Elements of an Array.

For string type, the moreLikeThis and queryString operators don't 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.

Note

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

To index a field as multiple types, define the types in the field definition array for the field.

Example

The following example shows the field definition for indexing a field as multiple types.

1{
2 ...
3 "mappings": {
4 "dynamic": <boolean>,
5 "fields": {
6 "<field-name>": [
7 {
8 "type": "<field-type>",
9 ...
10 },
11 {
12 "type": "<field-type>",
13 ...
14 },
15 ...
16 ],
17 ...
18 },
19 ...
20 }
21}

The following index definition example uses static mappings.

  • The default index analyzer is lucene.standard.

  • The default search analyzer is lucene.standard. You can change the search analyzer if you want the query term to be parsed differently than how it is stored in your MongoDB Search index.

  • The index specifies static field mappings (dynamic: false), which means fields that are not explicitly mentioned are not indexed. So, the index definition includes:

    • The address field, which is of type document. It has two embedded sub-fields, city and state.

      The city sub-field uses the lucene.simple analyzer by default for queries. It uses the ignoreAbove option to ignore any string of more than 255 bytes in length.

      The state sub-field uses the lucene.english analyzer by default for queries.

    • The company field, which is of type string. It uses the lucene.whitespace analyzer by default for queries. It has a multi analyzer named mySecondaryAnalyzer which uses the lucene.french analyzer by default for queries.

    • The employees field, which is an array of strings. It uses the lucene.standard analyzer by default for queries. 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.

{
"analyzer": "lucene.standard",
"searchAnalyzer": "lucene.standard",
"mappings": {
"dynamic": false,
"fields": {
"address": {
"type": "document",
"fields": {
"city": {
"type": "string",
"analyzer": "lucene.simple",
"ignoreAbove": 255
},
"state": {
"type": "string",
"analyzer": "lucene.english"
}
}
},
"company": {
"type": "string",
"analyzer": "lucene.whitespace",
"multi": {
"mySecondaryAnalyzer": {
"type": "string",
"analyzer": "lucene.french"
}
}
},
"employees": {
"type": "string",
"analyzer": "lucene.standard"
}
}
}
}

The following index definition example uses both static and dynamic mappings.

  • The default index analyzer is lucene.standard.

  • The default search analyzer is lucene.standard. You can change the search analyzer if you want the query term to be parsed differently than how it is stored in your MongoDB Search index.

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

    • The company field, which is of type string. It uses the lucene.whitespace analyzer by default for queries. It has a multi analyzer named mySecondaryAnalyzer which uses the lucene.french analyzer by default for queries.

    • The employees field, which is an array of strings. It uses the lucene.standard analyzer by default for queries.

    • The address field, which is of type document. It has two embedded sub-fields, city and state. Instead of explicitly mentioning each nested field in the document, the index definition enables dynamic mapping for all the sub-fields in the document. It uses the lucene.standard analyzer by default for queries.

{
"analyzer": "lucene.standard",
"searchAnalyzer": "lucene.standard",
"mappings": {
"dynamic": false,
"fields": {
"company": {
"type": "string",
"analyzer": "lucene.whitespace",
"multi": {
"mySecondaryAnalyzer": {
"type": "string",
"analyzer": "lucene.french"
}
}
},
"employees": {
"type": "string",
"analyzer": "lucene.standard"
},
"address": {
"type": "document",
"dynamic": true
}
}
}
}

Back

Token Filters

On this page