string-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: basic, multi, vib, jsonib, maven bom, gradle bom, maven, gradle -->

How to Index String Fields

Use the MongoDB Search string type to index [string](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-string fields. Use the MongoDB Search [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref, [querystring](https://www.mongodb.com/docs/search/query/operators-collectors/querystring.md#std-label-querystring-ref, [span](https://www.mongodb.com/docs/search/query/operators-collectors/span.md#std-label-span-ref, [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref, [wildcard](https://www.mongodb.com/docs/search/query/operators-collectors/wildcard.md#std-label-wildcard-ref, [regex](https://www.mongodb.com/docs/search/query/operators-collectors/regex.md#std-label-regex-ref, and [moreLikeThis](https://www.mongodb.com/docs/search/query/operators-collectors/morelikethis.md#std-label-more-like-this-ref operators to query fields indexed as the string type.

string Type Considerations

The string type doesn't support all MongoDB Search features for string fields. Index the string field as a [token](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-bson-data-types-token to:

  • Run a [facet](https://www.mongodb.com/docs/search/query/operators-collectors/facet.md#std-label-fts-facet-ref query on string fields.

  • Sort the MongoDB Search results by string fields.

  • Find an exact match for queries using [equals](https://www.mongodb.com/docs/search/query/operators-collectors/equals.md#std-label-equals-ref, [in](https://www.mongodb.com/docs/search/query/operators-collectors/in.md#std-label-in-ref, and [range](https://www.mongodb.com/docs/search/query/operators-collectors/range.md#std-label-range-ref operators.

The MongoDB Search [dynamic mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-fts-enable-dynamic-mappings with the default typeSet only indexes string fields as string types. Specify a custom typeSet to dynamically index strings as token or autocomplete to use their supported features.

Important:

MongoDB Search won't index string fields where analyzer tokens exceed 32766 bytes in size. If using the keyword analyzer, string fields which exceed 32766 bytes will not be indexed.

Define the Index for the string Type

  1. 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

  1. 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 string 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": "<collection>", "database": "<database>", "name": "default", "type": "search", "definition": { "mappings": { "dynamic": true|false, "fields": { "<field-name>": { "type": "string", "analyzer": "<atlas-search-analyzer>", "searchAnalyzer": "<atlas-search-analyzer>", "indexOptions": "docs|freqs|positions|offsets", "store": true|false, "ignoreAbove": <integer>, "similarity": { "type": "bm25|boolean|stableTfl" }, "multi": {<string-field-definition>}, "norms": "include|omit" } } } } }'

Configure string Field Properties

The MongoDB Search string type takes the following parameters:

Option Type Necessity Description Default
type string Required Human-readable label that identifies this field type. Value must be string.
analyzer string Optional Name of a built-in or custom analyzer.
searchAnalyzer string Optional Analyzer to use when querying the field. If you don't specify a value, inherits an analyzer by default in the following order: The analyzer option for this field if specified.The searchAnalyzer option for the [index](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions if specified.The analyzer option for the [index](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions if specified.The lucene.standard analyzer.
indexOptions string Optional Amount of information to store for the indexed field. Value can be one of the following: docs - Only indexes documents. The frequency and position of the indexed term are ignored. Only a single occurence of the term is reflected in the score.. offsets
store boolean Optional Flag that indicates whether to store the exact document text as well as the analyzed values in the index. Value can be true or false. The value for this option must be true for highlight.. true
ignoreAbove int Optional Maximum number of characters in the value of the field to index. MongoDB Search doesn't index if the field value is greater than the specified number of characters.
similarity.type String Optional Name of the similarity algorithm to use with this string mapping when scoring with the [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref, [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref, or [queryString](https://www.mongodb.com/docs/search/query/operators-collectors/querystring.md#std-label-querystring-ref operators. Value can be one of the following: bm25, boolean, or stableTfl. To learn more about the available similarity algorithms, see [Score Details.](https://www.mongodb.com/docs/search/query/score/get-details.md#std-label-fts-similarity-algorithms bm25
multi String Field Definition Optional String field to index with the name of the alternate analyzer specified in the multi object. To learn more about specifying the multi object, see [Multi Analyzer](https://www.mongodb.com/docs/search/indexes/analyzers/multi.md#std-label-ref-multi-analyzers and an example below.
norms string Optional String that specifies whether to include or omit the field length in the result when scoring. The length of the field is determined by the number of tokens produced by the analyzer for the field. Value can be one of the following: include - to include the field length when scoring.; omit - to omit the field length when scoring. If value is include, MongoDB Search uses the length of the field to determine the higher score when scoring. For example, if two documents match a MongoDB Search query, the document with the shorter field length scores higher than the document with the longer field length. If value is omit, MongoDB Search ignores the field length when scoring. include

Try an Example for the string 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 string 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.

Basic Example

The following index definition for the sample_mflix.movies collection in the [sample dataset](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets indexes the title field with string values.

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": {
              "title": {
                "type": "string"
              }
            }
          }
        }
    }'

Output:

{"collectionName":"movies","database":"sample_mflix","indexID":"<indexID>","latestDefinition":{"mappings":{"dynamic":true,"fields":{"title":{"type":"string"}}}},"latestDefinitionVersion":{"version":0},"name":"default","queryable":false,"status":"PENDING","type":"search"}

Define the Index for the string Type

  1. Create a search-index.json configuration file and specify the number type.

json { "name": "default", "collectionName": "<collection>", "database": "<database>", "definition": { "mappings": { "dynamic": true|false, "fields": { "<field-name>": { "type": "string", "analyzer": "<atlas-search-analyzer>", "searchAnalyzer": "<atlas-search-analyzer>", "indexOptions": "docs|freqs|positions|offsets", "store": true|false, "ignoreAbove": <integer>, "similarity": { "type": "bm25|boolean|stableTfl" }, "multi": { <string-field-definition> }, "norms": "include|omit" } } } } } }

  1. 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.

  1. 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 <cluster-name> --file <path-to-file>

Configure string Field Properties

The MongoDB Search string type takes the following parameters:

Option Type Necessity Description Default
type string Required Human-readable label that identifies this field type. Value must be string.
analyzer string Optional Name of a built-in or custom analyzer.
searchAnalyzer string Optional Analyzer to use when querying the field. If you don't specify a value, inherits an analyzer by default in the following order: The analyzer option for this field if specified.The searchAnalyzer option for the [index](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions if specified.The analyzer option for the [index](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions if specified.The lucene.standard analyzer.
indexOptions string Optional Amount of information to store for the indexed field. Value can be one of the following: docs - Only indexes documents. The frequency and position of the indexed term are ignored. Only a single occurence of the term is reflected in the score.. offsets
store boolean Optional Flag that indicates whether to store the exact document text as well as the analyzed values in the index. Value can be true or false. The value for this option must be true for highlight.. true
ignoreAbove int Optional Maximum number of characters in the value of the field to index. MongoDB Search doesn't index if the field value is greater than the specified number of characters.
similarity.type String Optional Name of the similarity algorithm to use with this string mapping when scoring with the [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref, [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref, or [queryString](https://www.mongodb.com/docs/search/query/operators-collectors/querystring.md#std-label-querystring-ref operators. Value can be one of the following: bm25, boolean, or stableTfl. To learn more about the available similarity algorithms, see [Score Details.](https://www.mongodb.com/docs/search/query/score/get-details.md#std-label-fts-similarity-algorithms bm25
multi String Field Definition Optional String field to index with the name of the alternate analyzer specified in the multi object. To learn more about specifying the multi object, see [Multi Analyzer](https://www.mongodb.com/docs/search/indexes/analyzers/multi.md#std-label-ref-multi-analyzers and an example below.
norms string Optional String that specifies whether to include or omit the field length in the result when scoring. The length of the field is determined by the number of tokens produced by the analyzer for the field. Value can be one of the following: include - to include the field length when scoring.; omit - to omit the field length when scoring. If value is include, MongoDB Search uses the length of the field to determine the higher score when scoring. For example, if two documents match a MongoDB Search query, the document with the shorter field length scores higher than the document with the longer field length. If value is omit, MongoDB Search ignores the field length when scoring. include

Try an Example for the string 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 string 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.

Basic Example

The following index definition for the sample_mflix.movies collection in the [sample dataset](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets indexes the title field with string values.

{
  "name": "default",
  "collectionName": "movies",
  "database": "sample_mflix",
  "definition": {
    "mappings": {
      "dynamic": false,
      "fields": {
        "title": {
          "type": "string"
        }
      }
    }
  }
}

Define the Index for the string Type

  1. 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

  1. Specify the database and collection.

On the Database screen, click the name of the database, then click the name of the collection.

  1. 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, "fields": { "<field-name>": { "type": "string", "analyzer": "<atlas-search-analyzer>", "searchAnalyzer": "<atlas-search-analyzer>", "indexOptions": "docs|freqs|positions|offsets", "store": true|false, "ignoreAbove": <integer>, "similarity": { "type": "bm25|boolean|stableTfl" }, "multi": { <string-field-definition> }, "norms": "include|omit" } } } }

Click Create Search Index.

Configure string Field Properties

The MongoDB Search string type takes the following parameters:

Option Type Necessity Description Default
type string Required Human-readable label that identifies this field type. Value must be string.
analyzer string Optional Name of a built-in or custom analyzer.
searchAnalyzer string Optional Analyzer to use when querying the field. If you don't specify a value, inherits an analyzer by default in the following order: The analyzer option for this field if specified.The searchAnalyzer option for the [index](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions if specified.The analyzer option for the [index](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions if specified.The lucene.standard analyzer.
indexOptions string Optional Amount of information to store for the indexed field. Value can be one of the following: docs - Only indexes documents. The frequency and position of the indexed term are ignored. Only a single occurence of the term is reflected in the score.. offsets
store boolean Optional Flag that indicates whether to store the exact document text as well as the analyzed values in the index. Value can be true or false. The value for this option must be true for highlight.. true
ignoreAbove int Optional Maximum number of characters in the value of the field to index. MongoDB Search doesn't index if the field value is greater than the specified number of characters.
similarity.type String Optional Name of the similarity algorithm to use with this string mapping when scoring with the [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref, [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref, or [queryString](https://www.mongodb.com/docs/search/query/operators-collectors/querystring.md#std-label-querystring-ref operators. Value can be one of the following: bm25, boolean, or stableTfl. To learn more about the available similarity algorithms, see [Score Details.](https://www.mongodb.com/docs/search/query/score/get-details.md#std-label-fts-similarity-algorithms bm25
multi String Field Definition Optional String field to index with the name of the alternate analyzer specified in the multi object. To learn more about specifying the multi object, see [Multi Analyzer](https://www.mongodb.com/docs/search/indexes/analyzers/multi.md#std-label-ref-multi-analyzers and an example below.
norms string Optional String that specifies whether to include or omit the field length in the result when scoring. The length of the field is determined by the number of tokens produced by the analyzer for the field. Value can be one of the following: include - to include the field length when scoring.; omit - to omit the field length when scoring. If value is include, MongoDB Search uses the length of the field to determine the higher score when scoring. For example, if two documents match a MongoDB Search query, the document with the shorter field length scores higher than the document with the longer field length. If value is omit, MongoDB Search ignores the field length when scoring. include

Try an Example for the string 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 string 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.

Basic Example

The following index definition for the sample_mflix.movies collection in the [sample dataset](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets indexes the title field with string values.

{
  "mappings": {
    "dynamic": false,
    "fields": {
        "title": {
            "type": "string"
        }
    }
  }
}

Define the Index for the string Type

  1. 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.

  1. 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>

  1. Run the db.collection.createSearchIndex() method to create the index.

shell db.<collection>.createSearchIndex( "default", { "mappings": { "dynamic": true|false, "fields": { "<field-name>": { "type": "string" "analyzer": "<atlas-search-analyzer>", "searchAnalyzer": "<atlas-search-analyzer>", "indexOptions": "docs|freqs|positions|offsets", "store": true|false, "ignoreAbove": <integer>, "similarity": { "type": "bm25|boolean|stableTfl" } "multi": { <string-field-definition> }, "norms": "include|omit" }, } } } )

Output:

text default

Configure string Field Properties

The MongoDB Search string type takes the following parameters:

Option Type Necessity Description Default
type string Required Human-readable label that identifies this field type. Value must be string.
analyzer string Optional Name of a built-in or custom analyzer.
searchAnalyzer string Optional Analyzer to use when querying the field. If you don't specify a value, inherits an analyzer by default in the following order: The analyzer option for this field if specified.The searchAnalyzer option for the [index](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions if specified.The analyzer option for the [index](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions if specified.The lucene.standard analyzer.
indexOptions string Optional Amount of information to store for the indexed field. Value can be one of the following: docs - Only indexes documents. The frequency and position of the indexed term are ignored. Only a single occurence of the term is reflected in the score.. offsets
store boolean Optional Flag that indicates whether to store the exact document text as well as the analyzed values in the index. Value can be true or false. The value for this option must be true for highlight.. true
ignoreAbove int Optional Maximum number of characters in the value of the field to index. MongoDB Search doesn't index if the field value is greater than the specified number of characters.
similarity.type String Optional Name of the similarity algorithm to use with this string mapping when scoring with the [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref, [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref, or [queryString](https://www.mongodb.com/docs/search/query/operators-collectors/querystring.md#std-label-querystring-ref operators. Value can be one of the following: bm25, boolean, or stableTfl. To learn more about the available similarity algorithms, see [Score Details.](https://www.mongodb.com/docs/search/query/score/get-details.md#std-label-fts-similarity-algorithms bm25
multi String Field Definition Optional String field to index with the name of the alternate analyzer specified in the multi object. To learn more about specifying the multi object, see [Multi Analyzer](https://www.mongodb.com/docs/search/indexes/analyzers/multi.md#std-label-ref-multi-analyzers and an example below.
norms string Optional String that specifies whether to include or omit the field length in the result when scoring. The length of the field is determined by the number of tokens produced by the analyzer for the field. Value can be one of the following: include - to include the field length when scoring.; omit - to omit the field length when scoring. If value is include, MongoDB Search uses the length of the field to determine the higher score when scoring. For example, if two documents match a MongoDB Search query, the document with the shorter field length scores higher than the document with the longer field length. If value is omit, MongoDB Search ignores the field length when scoring. include

Try an Example for the string 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 string 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.

Basic Example

The following index definition for the sample_mflix.movies collection in the [sample dataset](https://www.mongodb.com/docs/manual/sample-data.md#std-label-available-sample-datasets indexes the title field with string values.

db.movies.createSearchIndex(
  {
    "mappings": {
      "dynamic": false,
      "fields": {
        "title": {
          "type": "string"
        }
      }
    }
  }
)

Define the Index for the string Type

Choose your preferred configuration method in the Atlas UI and then select the database and collection.

Visual Editor

  1. Click Refine Your Index to configure your index.

  2. In the Field Mappings section, click Add Field Mapping to open the Add Field Mapping window.

  3. Click Customized Configuration.

  4. 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.

  1. Click the Data Type dropdown and select String.

  2. Configure the field properties for the string type. 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.

  3. Click Add.

Note:

To configure the similarity.type property for string fields, use the JSON (Javascript Object Notation) Editor.

Configure string Field Properties

The MongoDB Search string type takes the following parameters:

Option Type Necessity Description Default
type string Required Human-readable label that identifies this field type. Value must be string.
analyzer string Optional Name of a built-in or custom analyzer.
searchAnalyzer string Optional Analyzer to use when querying the field. If you don't specify a value, inherits an analyzer by default in the following order: The analyzer option for this field if specified.The searchAnalyzer option for the [index](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions if specified.The analyzer option for the [index](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions if specified.The lucene.standard analyzer.
indexOptions string Optional Amount of information to store for the indexed field. Value can be one of the following: docs - Only indexes documents. The frequency and position of the indexed term are ignored. Only a single occurence of the term is reflected in the score.. offsets
store boolean Optional Flag that indicates whether to store the exact document text as well as the analyzed values in the index. Value can be true or false. The value for this option must be true for highlight.. true
ignoreAbove int Optional Maximum number of characters in the value of the field to index. MongoDB Search doesn't index if the field value is greater than the specified number of characters.
similarity.type String Optional Name of the similarity algorithm to use with this string mapping when scoring with the [text](https://www.mongodb.com/docs/search/query/operators-collectors/text.md#std-label-text-ref, [phrase](https://www.mongodb.com/docs/search/query/operators-collectors/phrase.md#std-label-phrase-ref, or [queryString](https://www.mongodb.com/docs/search/query/operators-collectors/querystring.md#std-label-querystring-ref operators. Value can be one of the following: bm25, boolean, or stableTfl. To learn more about the available similarity algorithms, see [Score Details.](https://www.mongodb.com/docs/search/query/score/get-details.md#std-label-fts-similarity-algorithms bm25
multi String Field Definition Optional String field to index with the name of the alternate analyzer specified in the multi object. To learn more about specifying the multi object, see [Multi Analyzer](https://www.mongodb.com/docs/search/indexes/analyzers/multi.md#std-label-ref-multi-analyzers and an example belo

(本文超出站点单文档体积上限,已截断)