document-type
- document-type
- How to Index Fields in Objects and Documents
- document Type Limitations
- Define the Index for the document Type
- Configure document Field Properties
- Try an Example for the document Type
- document Type Limitations
- Define the Index for the document Type
- Configure document Field Properties
- Try an Example for the document Type
- document Type Limitations
- Define the Index for the document Type
- Configure document Field Properties
- Try an Example for the document Type
- document Type Limitations
- Define the Index for the document Type
- Configure document Field Properties
- Try an Example for the document Type
- document Type Limitations
- Define the Index for the document Type
- Configure document Field Properties
- Try an Example for the document Type
- document Type Limitations
- Define the Index for the document Type
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset
Available tabs: other tabs: vib, jsonib, cloud, local, maven bom, gradle bom, maven, gradle -->
How to Index Fields in Objects and Documents
Use the MongoDB Search document field type to index fields in objects or subdocuments. When you enable dynamic mappings on a document type field, MongoDB Search automatically indexes all dynamically indexable field types nested within that field. You can also configure static mappings to index only specific nested field types.
You can use the MongoDB Search document type to index fields in objects or documents.
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes all dynamically indexable fields types in a document type field. Alternatively, you can configure dynamic mappings to index only specific field types nested in a document type field.
You can use the Visual Editor or the JSON Editor in the Atlas UI, Atlas CLI, Atlas Administration API, [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh, MongoDB Compass, or MongoDB drivers to index fields as the document type.
document Type Limitations
You can't use the MongoDB Search document type to index fields in objects or documents that are inside an array. Instead, use the MongoDB Search [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type to index fields in objects or documents that are elements of an array.
Define the Index for the document Type
- Retrieve your Service Account access token for the project.
Use your service account to generate an access token, which authenticates your requests to the Atlas Administration API. To learn how to generate an access token, see [Make an API (Application Programming Interface) Request.](https://www.mongodb.com/docs/atlas/configure-api-access.md#std-label-example-api-request
- Make an API (Application Programming Interface) call.
Replace the following values in the curl command below. This command sends a POST request to your cluster to create a MongoDB Search index with the document field type.
-
Replace {
ACCESS-TOKEN} with the output from the preceding step -
Replace {
groupId} with the project ID of the project where you want to create the MongoDB Search index -
Replace {
clusterName} with the name of the cluster where you want to create the MongoDB Search index
```shell
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header "Accept: application/vnd.atlas.2025-03-12+json" \
--header "Content-Type: application/json" \
-X POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \
-d '{ "collectionName": "
```
Configure document Field Properties
The MongoDB Search document type takes the following parameters:
| Option | Type | Necessity | Description | Default |
|---|---|---|---|---|
type |
string | Required | Human-readable label that identifies the field type. Value must be document. |
|
dynamic |
boolean or object | Optional | Flag that indicates whether MongoDB Search recursively indexes all fields and nested documents or object that references the name of the field type definition that contains the field types to index. Value must be one of the following: boolean - set to true to recursively index all indexable field types or set to false to not dynamically index any of the dynamically indexable field types. For the list of field types that MongoDB Search dynamically indexes using the default type set, see [Enable Default typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-enable-dynamic-mappings; object - specify the name typeSets that contains the list of field types to index. To learn more, see dynamic.typeSet. If you set this to false, you must specify individual fields to index using fields object. You can also configure individual fields to index separately using the fields object. IMPORTANT: MongoDB Search automatically indexes all dynamically indexable field types in a document. MongoDB Search also recursively indexes all nested documents under the document, unless you explicitly override by setting dynamic to false. You can also configure dynamic indexing to only index specified field types using typeSets. |
false |
dynamic.typeSet |
string | Optional | Name of the typeSet array of objects that configures the field types to index. To learn more, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
|
fields |
document | Optional | Document that maps field names to field definitions. To learn more, see an [example](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-index-config-example. This is required if dynamic is omitted or set to false. |
|
typeSets |
array of objects | Optional | Field types to index automatically using dynamic mappings. To learn about the field types that you can configure for dynamic mappings, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
|
typeSets.[n].name |
string | Required | Human-readable label that identifies the configuration for the fields types to dynamically index. | |
typeSets.[n].types |
array of objects | Required | Field types, one per object, to index automatically using dynamic mappings. | |
typeSets.[n].types.[n].type |
string | Required | Field type to automatically index. You can also configure the settings for each field type. If you don't configure any settings, MongoDB Search uses the default values for the field type. To learn more about the field types that you can configure for dynamic mapping, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
Try an Example for the document Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the document type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
Index Dynamically Using Default typeSet
Enables dynamic mappings to index all dynamically indexable field types.
The following index definition for the sample_mflix.movies collection indexes the awards field as the document type. It also configures MongoDB Search to automatically index all the dynamically indexable fields inside the awards object.
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header "Accept: application/vnd.atlas.2025-03-12+json" \
--header "Content-Type: application/json" \
-X POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \
-d '{ "collectionName": "movies",
"database": "sample_mflix",
"name": "default",
"type": "search",
"definition": {
"mappings": {
"dynamic": false,
"fields": {
"awards": {
"type": "document",
"dynamic": true
}
}
}
}
}'
Index Dynamically Using Configured typeSet
Configures field types to dynamically index.
The following index definition for the sample_mflix.movies collection indexes the awards field as the document type. It references the typeSet name, onlyNumbers. The field type definition named onlyNumbers enables automatic indexing for only fields of type number in the awards document.
Note:
To configure typeSet using the Atlas Administration API for dynamic mappings, you must use API (Application Programming Interface) version 2025-09-10 or higher.
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header "Accept: application/vnd.atlas.2025-03-12+json" \
--header "Content-Type: application/json" \
-X POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \
-d '{ "collectionName": "movies",
"database": "sample_mflix",
"name": "default",
"type": "search",
"definition": {
"mappings": {
"dynamic": false,
"fields": {
"awards": {
"type": "document",
"dynamic": {
"typeSet": "onlyNumbers"
}
}
}
},
"typeSets": [
{
"name": "onlyNumbers",
"types": [
{
"type": "number"
}
]
}
]
}
}'
You can use the MongoDB Search document type to index fields in objects or documents.
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes all dynamically indexable fields types in a document type field. Alternatively, you can configure dynamic mappings to index only specific field types nested in a document type field.
You can use the Visual Editor or the JSON Editor in the Atlas UI, Atlas CLI, Atlas Administration API, [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh, MongoDB Compass, or MongoDB drivers to index fields as the document type.
document Type Limitations
You can't use the MongoDB Search document type to index fields in objects or documents that are inside an array. Instead, use the MongoDB Search [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type to index fields in objects or documents that are elements of an array.
Define the Index for the document Type
- Create a
search-index.jsonconfiguration file and specify thedocumenttype.
```json
{
"name": "default",
"collectionName": "
```
- Connect to the Atlas CLI
In your terminal, connect to your cluster from the Atlas CLI. To learn more, see Connect from the Atlas CLI.
- Create a MongoDB Search index.
Run the atlas clusters search indexes create command in your terminal, replacing <clusterName> with the name of the cluster, and <path-to-file> with the path to the search-index.json file:
```shell
atlas clusters search indexes create --clusterName
```
Configure document Field Properties
The MongoDB Search document type takes the following parameters:
| Option | Type | Necessity | Description | Default |
|---|---|---|---|---|
type |
string | Required | Human-readable label that identifies the field type. Value must be document. |
|
dynamic |
boolean or object | Optional | Flag that indicates whether MongoDB Search recursively indexes all fields and nested documents or object that references the name of the field type definition that contains the field types to index. Value must be one of the following: boolean - set to true to recursively index all indexable field types or set to false to not dynamically index any of the dynamically indexable field types. For the list of field types that MongoDB Search dynamically indexes using the default type set, see [Enable Default typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-enable-dynamic-mappings; object - specify the name typeSets that contains the list of field types to index. To learn more, see dynamic.typeSet. If you set this to false, you must specify individual fields to index using fields object. You can also configure individual fields to index separately using the fields object. IMPORTANT: MongoDB Search automatically indexes all dynamically indexable field types in a document. MongoDB Search also recursively indexes all nested documents under the document, unless you explicitly override by setting dynamic to false. You can also configure dynamic indexing to only index specified field types using typeSets. |
false |
dynamic.typeSet |
string | Optional | Name of the typeSet array of objects that configures the field types to index. To learn more, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
|
fields |
document | Optional | Document that maps field names to field definitions. To learn more, see an [example](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-index-config-example. This is required if dynamic is omitted or set to false. |
|
typeSets |
array of objects | Optional | Field types to index automatically using dynamic mappings. To learn about the field types that you can configure for dynamic mappings, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
|
typeSets.[n].name |
string | Required | Human-readable label that identifies the configuration for the fields types to dynamically index. | |
typeSets.[n].types |
array of objects | Required | Field types, one per object, to index automatically using dynamic mappings. | |
typeSets.[n].types.[n].type |
string | Required | Field type to automatically index. You can also configure the settings for each field type. If you don't configure any settings, MongoDB Search uses the default values for the field type. To learn more about the field types that you can configure for dynamic mapping, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
Try an Example for the document Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the document type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
Index Dynamically Using Default typeSet
Enables dynamic mappings to index all dynamically indexable field types.
The following index definition for the sample_mflix.movies collection indexes the awards field as the document type. It also configures MongoDB Search to automatically index all the dynamically indexable fields inside the awards object.
{
"name": "default",
"collectionName": "movies",
"database": "sample_mflix",
"mappings": {
"dynamic": false,
"fields": {
"awards": {
"type": "document",
"dynamic": true
}
}
}
}
Index Dynamically Using Configured typeSet
Configures field types to dynamically index.
The following index definition for the sample_mflix.movies collection indexes the awards field as the document type. It references the typeSet name, onlyNumbers. The field type definition named onlyNumbers enables automatic indexing for only fields of type number in the awards document.
{
"name": "default",
"collectionName": "movies",
"database": "sample_mflix",
"mappings": {
"dynamic": false,
"fields": {
"awards": {
"type": "document",
"dynamic": {
"typeSet": "onlyNumbers"
}
}
}
},
"typeSets": [
{
"name": "onlyNumbers",
"types": [
{
"type": "number"
}
]
}
]
}
You can use the MongoDB Search document type to index fields in objects or documents.
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes all dynamically indexable fields types in a document type field. Alternatively, you can configure dynamic mappings to index only specific field types nested in a document type field.
You can use the Visual Editor or the JSON Editor in the Atlas UI, Atlas CLI, Atlas Administration API, [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh, MongoDB Compass, or MongoDB drivers to index fields as the document type.
document Type Limitations
You can't use the MongoDB Search document type to index fields in objects or documents that are inside an array. Instead, use the MongoDB Search [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type to index fields in objects or documents that are elements of an array.
Define the Index for the document Type
- Connect to your cluster using MongoDB Compass.
Open Compass and connect to your cluster. For detailed instructions, see [Connect to a Cluster via Compass.](https://www.mongodb.com/docs/atlas/compass-connection.md#std-label-atlas-connect-via-compass
- Specify the database and collection.
On the Database screen, click the name of the database, then click the name of the collection.
- Create the MongoDB Search index.
Click the Indexes tab, then select Search Indexes.
Click Create Atlas Search Index to open the index creation dialog box.
Specify a name for the index.
Your MongoDB Search index is named default by default. If you keep this name, then your index will be the default Search index for any MongoDB Search query that does not specify a different index option in its [operators](https://www.mongodb.com/docs/search/query/operators-collectors/overview.md#std-label-fts-operators. If you are creating multiple indexes, we recommend that you maintain a consistent, descriptive naming convention across your indexes.
Specify the JSON (Javascript Object Notation) MongoDB Search index definition.
```json
{
"mappings": {
"dynamic": true|false | {
"typeSeet": "
```
Click Create Search Index.
Configure document Field Properties
The MongoDB Search document type takes the following parameters:
| Option | Type | Necessity | Description | Default |
|---|---|---|---|---|
type |
string | Required | Human-readable label that identifies the field type. Value must be document. |
|
dynamic |
boolean or object | Optional | Flag that indicates whether MongoDB Search recursively indexes all fields and nested documents or object that references the name of the field type definition that contains the field types to index. Value must be one of the following: boolean - set to true to recursively index all indexable field types or set to false to not dynamically index any of the dynamically indexable field types. For the list of field types that MongoDB Search dynamically indexes using the default type set, see [Enable Default typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-enable-dynamic-mappings; object - specify the name typeSets that contains the list of field types to index. To learn more, see dynamic.typeSet. If you set this to false, you must specify individual fields to index using fields object. You can also configure individual fields to index separately using the fields object. IMPORTANT: MongoDB Search automatically indexes all dynamically indexable field types in a document. MongoDB Search also recursively indexes all nested documents under the document, unless you explicitly override by setting dynamic to false. You can also configure dynamic indexing to only index specified field types using typeSets. |
false |
dynamic.typeSet |
string | Optional | Name of the typeSet array of objects that configures the field types to index. To learn more, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
|
fields |
document | Optional | Document that maps field names to field definitions. To learn more, see an [example](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-index-config-example. This is required if dynamic is omitted or set to false. |
|
typeSets |
array of objects | Optional | Field types to index automatically using dynamic mappings. To learn about the field types that you can configure for dynamic mappings, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
|
typeSets.[n].name |
string | Required | Human-readable label that identifies the configuration for the fields types to dynamically index. | |
typeSets.[n].types |
array of objects | Required | Field types, one per object, to index automatically using dynamic mappings. | |
typeSets.[n].types.[n].type |
string | Required | Field type to automatically index. You can also configure the settings for each field type. If you don't configure any settings, MongoDB Search uses the default values for the field type. To learn more about the field types that you can configure for dynamic mapping, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
Try an Example for the document Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the document type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
Index Dynamically Using Default typeSet
Enables dynamic mappings to index all dynamically indexable field types.
The following index definition for the sample_mflix.movies collection indexes the awards field as the document type. It also configures MongoDB Search to automatically index all the dynamically indexable fields inside the awards object.
{
"mappings": {
"dynamic": false,
"fields": {
"awards": {
"type": "document",
"dynamic": true
}
}
}
}
Index Dynamically Using Configured typeSet
Configures field types to dynamically index.
The following index definition for the sample_mflix.movies collection indexes the awards field as the document type. It references the typeSet name, onlyNumbers. The field type definition named onlyNumbers enables automatic indexing for only fields of type number in the awards document.
{
"mappings": {
"dynamic": false,
"fields": {
"awards": {
"type": "document",
"dynamic": {
"typeSet": "onlyNumbers"
}
}
}
},
"typeSets": [
{
"name": "onlyNumbers",
"types": [
{
"type": "number"
}
]
}
]
}
You can use the MongoDB Search document type to index fields in objects or documents.
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes all dynamically indexable fields types in a document type field. Alternatively, you can configure dynamic mappings to index only specific field types nested in a document type field.
You can use the Visual Editor or the JSON Editor in the Atlas UI, Atlas CLI, Atlas Administration API, [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh, MongoDB Compass, or MongoDB drivers to index fields as the document type.
document Type Limitations
You can't use the MongoDB Search document type to index fields in objects or documents that are inside an array. Instead, use the MongoDB Search [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type to index fields in objects or documents that are elements of an array.
Define the Index for the document Type
- Connect to the deployment by using
mongosh.
In your terminal, connect to your Atlas cloud-hosted deployment or local deployment from [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh. For detailed instructions on how to connect, see Connect to a Deployment.
- Switch to the database that contains the collection for which you want to create the index.
shell
use <database>
Output:
shell
switched to db <database>
- Run the
db.collection.createSearchIndex()method to create the index.
shell
db.<collection>.createSearchIndex(
"default",
{
"mappings": {
"dynamic": true|false,
"fields": {
"<field-name>": {
"type": "document",
"dynamic": true|false | {
"typeSet": "<typeSet-name>"
},
"fields": {
"<sub-field-name>": {
<field-mapping-definition>
},
...
}
}
}
},
"typeSets": [
{
"name": "<typeSet-name>",
"types": [
{
"type": "<field-type>",
...
},
...
]
},
...
]
}
)
Output:
text
default
Configure document Field Properties
The MongoDB Search document type takes the following parameters:
| Option | Type | Necessity | Description | Default |
|---|---|---|---|---|
type |
string | Required | Human-readable label that identifies the field type. Value must be document. |
|
dynamic |
boolean or object | Optional | Flag that indicates whether MongoDB Search recursively indexes all fields and nested documents or object that references the name of the field type definition that contains the field types to index. Value must be one of the following: boolean - set to true to recursively index all indexable field types or set to false to not dynamically index any of the dynamically indexable field types. For the list of field types that MongoDB Search dynamically indexes using the default type set, see [Enable Default typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-enable-dynamic-mappings; object - specify the name typeSets that contains the list of field types to index. To learn more, see dynamic.typeSet. If you set this to false, you must specify individual fields to index using fields object. You can also configure individual fields to index separately using the fields object. IMPORTANT: MongoDB Search automatically indexes all dynamically indexable field types in a document. MongoDB Search also recursively indexes all nested documents under the document, unless you explicitly override by setting dynamic to false. You can also configure dynamic indexing to only index specified field types using typeSets. |
false |
dynamic.typeSet |
string | Optional | Name of the typeSet array of objects that configures the field types to index. To learn more, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
|
fields |
document | Optional | Document that maps field names to field definitions. To learn more, see an [example](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-index-config-example. This is required if dynamic is omitted or set to false. |
|
typeSets |
array of objects | Optional | Field types to index automatically using dynamic mappings. To learn about the field types that you can configure for dynamic mappings, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
|
typeSets.[n].name |
string | Required | Human-readable label that identifies the configuration for the fields types to dynamically index. | |
typeSets.[n].types |
array of objects | Required | Field types, one per object, to index automatically using dynamic mappings. | |
typeSets.[n].types.[n].type |
string | Required | Field type to automatically index. You can also configure the settings for each field type. If you don't configure any settings, MongoDB Search uses the default values for the field type. To learn more about the field types that you can configure for dynamic mapping, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
Try an Example for the document Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the document type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
Index Dynamically Using Default typeSet
Enables dynamic mappings to index all dynamically indexable field types.
The following index definition for the sample_mflix.movies collection indexes the awards field as the document type. It also configures MongoDB Search to automatically index all the dynamically indexable fields inside the awards object.
db.movies.createSearchIndex(
"default",
{
"mappings": {
"dynamic": false,
"fields": {
"awards": {
"type": "document",
"dynamic": true
}
}
}}
)
Index Dynamically Using Configured typeSet
Configures field types to dynamically index.
The following index definition for the sample_mflix.movies collection indexes the awards field as the document type. It references the typeSet name, onlyNumbers. The field type definition named onlyNumbers enables automatic indexing for only fields of type number in the awards document.
db.movies.createSearchIndex(
"default",
{
"mappings": {
"dynamic": false,
"fields": {
"awards": {
"type": "document",
"dynamic": {
"typeSet": "onlyNumbers"
}
}
}
},
"typeSets": [
{
"name": "onlyNumbers",
"types": [
{
"type": "number"
}
]
}
]
}
)
You can use the MongoDB Search document type to index fields in objects or documents.
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes all dynamically indexable fields types in a document type field. Alternatively, you can configure dynamic mappings to index only specific field types nested in a document type field.
You can use the Visual Editor or the JSON Editor in the Atlas UI, Atlas CLI, Atlas Administration API, [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh, MongoDB Compass, or MongoDB drivers to index fields as the document type.
document Type Limitations
You can't use the MongoDB Search document type to index fields in objects or documents that are inside an array. Instead, use the MongoDB Search [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type to index fields in objects or documents that are elements of an array.
Define the Index for the document Type
Choose your preferred configuration method in the Atlas UI and then select the database and collection.
Visual Editor
-
Click Refine Your Index to configure your index.
-
In the Field Mappings section, click Add Field Mapping to open the Add Field Mapping window.
-
Click Customized Configuration.
-
Select the field to index from the Field Name dropdown.
Note:
You can't index fields that contain the dollar ($) sign at the start of the field name.
-
Click the Data Type dropdown and select Document.
-
Configure the field properties for the
documenttype. To learn more, see [Field Properties](https://www.mongodb.com/docs/search/indexes/field-types/uuid-type.md#std-label-fts-field-types-uuid-options. -
Click Add.
Configure document Field Properties
The MongoDB Search document type takes the following parameters:
| Option | Type | Necessity | Description | Default |
|---|---|---|---|---|
type |
string | Required | Human-readable label that identifies the field type. Value must be document. |
|
dynamic |
boolean or object | Optional | Flag that indicates whether MongoDB Search recursively indexes all fields and nested documents or object that references the name of the field type definition that contains the field types to index. Value must be one of the following: boolean - set to true to recursively index all indexable field types or set to false to not dynamically index any of the dynamically indexable field types. For the list of field types that MongoDB Search dynamically indexes using the default type set, see [Enable Default typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-enable-dynamic-mappings; object - specify the name typeSets that contains the list of field types to index. To learn more, see dynamic.typeSet. If you set this to false, you must specify individual fields to index using fields object. You can also configure individual fields to index separately using the fields object. IMPORTANT: MongoDB Search automatically indexes all dynamically indexable field types in a document. MongoDB Search also recursively indexes all nested documents under the document, unless you explicitly override by setting dynamic to false. You can also configure dynamic indexing to only index specified field types using typeSets. |
false |
dynamic.typeSet |
string | Optional | Name of the typeSet array of objects that configures the field types to index. To learn more, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
|
fields |
document | Optional | Document that maps field names to field definitions. To learn more, see an [example](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-index-config-example. This is required if dynamic is omitted or set to false. |
|
typeSets |
array of objects | Optional | Field types to index automatically using dynamic mappings. To learn about the field types that you can configure for dynamic mappings, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
|
typeSets.[n].name |
string | Required | Human-readable label that identifies the configuration for the fields types to dynamically index. | |
typeSets.[n].types |
array of objects | Required | Field types, one per object, to index automatically using dynamic mappings. | |
typeSets.[n].types.[n].type |
string | Required | Field type to automatically index. You can also configure the settings for each field type. If you don't configure any settings, MongoDB Search uses the default values for the field type. To learn more about the field types that you can configure for dynamic mapping, see [Configure a typeSet.](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-configure-dynamic-mappings |
Try an Example for the document Type
The following index definition examples use collections in the [sample data](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets. After you [load the sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data, you can use these examples to index fields as the document type.
To learn how to set up and run the following example code, see [Define the Index for the token Type](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-define-index-token-admin-api.
Index Dynamically Using Default typeSet
Enables dynamic mappings to index all dynamically indexable field types.
The following index definition for the sample_mflix.movies collection indexes the awards field as the document type. It also configures MongoDB Search to automatically index all the dynamically indexable fields inside the awards object.
Visual Editor
-
In the Add Field Mapping window, select awards from the Field Name dropdown.
-
Click the Data Type dropdown and select Document.
-
Modify the Document Properties to set the value for Dynamic to
true. -
Click Add.
Index Dynamically Using Configured typeSet
Configures field types to dynamically index.
The following index definition for the sample_mflix.movies collection indexes the awards field as the document type. It references the typeSet name, onlyNumbers. The field type definition named onlyNumbers enables automatic indexing for only fields of type number in the awards document.
Visual Editor
You can't configure typeSets for dynamic mappings using the Atlas UI Visual Editor.
You can use the MongoDB Search document type to index fields in objects or documents.
If you enable [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings, MongoDB Search automatically indexes all dynamically indexable fields types in a document type field. Alternatively, you can configure dynamic mappings to index only specific field types nested in a document type field.
You can use the Visual Editor or the JSON Editor in the Atlas UI, Atlas CLI, Atlas Administration API, [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh, MongoDB Compass, or MongoDB drivers to index fields as the document type.
document Type Limitations
You can't use the MongoDB Search document type to index fields in objects or documents that are inside an array. Instead, use the MongoDB Search [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type to index fields in objects or documents that are elements of an array.
Define the Index for the document Type
- Set up the C project.
In your terminal, navigate to where you want to create your application, then run the following command to create a directory called atlas-search-project for this project:
shell
# Create a new directory for the project
mkdir atlas-search-project && cd atlas-search-project
Add the C driver to your project by following the instructions in the MongoDB C Driver documentation.
- Define the index.
Create a create_index.c file in your project directory, and copy and paste the following code into the file.
```c #include <mongoc/mongoc.h> #include <stdio.h>
int main(int argc, char argv[]) { mongoc_client_t client; mongoc_collection_t collection; bson_error_t error; bson_t cmd; bson_t index_definition; bool success;
/* Initialize the MongoDB C Driver. */
mongoc_init();
/* Connect to your MongoDB deployment. */
client = mongoc_client_new("<connection-string>");
collection = mongoc_client_get_collection (client, "<database>", "<collection>");
if (!client) {
fprintf(stderr, "Failed to create client.\n");
return 1;
}
/* Create the MongoDB Search index definition for the document field. */
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"createSearchIndexes": "%s",
"indexes": [{
"name": "default",
"definition": {
"mappings": {
"dynamic": true|false | { // "dynamic" can be a boolean or an obje
(本文超出站点单文档体积上限,已截断)
