Define Stored Source Fields in Your MongoDB Search Index
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
Define Stored Source Fields in Your MongoDB Search Index
The storedSource option in a MongoDB Search [index definition](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions specifies the fields in the source document that MongoDB Search must store. You can configure storedSource to improve query performance in certain [use cases](https://www.mongodb.com/docs/search/query/return-stored-source.md#std-label-fts-return-stored-source-use-case, as it reduces the need for implicit query time lookup on the backend database. You can store fields of all [MongoDB Search Field Types](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-bson-data-chart on MongoDB Search.
MongoDB Search doesn't index stored fields, so you must index the fields separately in order to run queries on them. You can also retrieve stored fields at query time by using the [returnStoredSource](https://www.mongodb.com/docs/search/query/return-stored-source.md#std-label-fts-return-stored-source-option option.
Note:
storedSource is only available on clusters running MongoDB 7.0+.
To learn more about retrieving the stored fields, see [Return Stored Source Fields.](https://www.mongodb.com/docs/search/query/return-stored-source.md#std-label-fts-return-stored-source-option
Syntax
The storedSource option has the following syntax in an [index definition:](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions
{
"storedSource": true | false | {
"include" | "exclude": [
"<field-name>",
...
]
}
}
Options
The storedSource option takes a [boolean value](https://www.mongodb.com/docs/search/indexes/stored-source-definition.md#std-label-fts-stored-source-boolean-values or an [object](https://www.mongodb.com/docs/search/indexes/stored-source-definition.md#std-label-fts-stored-source-option in the [index definition.](https://www.mongodb.com/docs/search/indexes/index-definitions.md#std-label-ref-index-definitions
Boolean Values
| Value | Description |
|---|---|
true |
Specifies that MongoDB Search must store all the fields in the documents. Storing full documents might significantly impact performance during indexing and querying. To learn more, see [Storing Source Fields.](https://www.mongodb.com/docs/search/performance/index-performance.md#std-label-perf-ref-stored-source This is not supported if index definition contains [vector](https://www.mongodb.com/docs/search/indexes/field-types/vector-type.md#std-label-bson-data-types-vector type field. Instead, use include to specify the fields to store on mongot or use exclude to exclude the vector type field from storage. |
false |
Specifies that MongoDB Search must not store the original source document. This is the default value for the storedSource option. |
Object
The storedSource option object must contain one of the following fields:
| Field | Type | Description |
|---|---|---|
include |
array of strings | List that contains the field names or dot-separated paths to fields to store. In addition to the specified fields, MongoDB Search stores _id also by default. |
exclude |
array of strings | List that contains the field names or dot-separated paths to fields to exclude from being stored. If specified, MongoDB Search stores original documents except the fields listed here. If index definition contains field of type [vector](https://www.mongodb.com/docs/search/indexes/field-types/vector-type.md#std-label-bson-data-types-vector, you must exclude the vector type field from storage. |
Examples
The following index examples use the fields in the sample_mflix.movies collection to demonstrate how to configure the fields to store on MongoDB Search using the storedSource option. You can use [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh, the Atlas UI Visual Editor, or the Atlas UI JSON Editor to configure the indexes.
The following example stores only the title and awards.wins fields in the documents in the collection. After you select your preferred configuration method, select the movies collection under the sample_mflix database.
Click Refine Your Index to configure your index.
In the Stored Source Fields section, click Specified.
Select awards.wins from the dropdown in the Field Name column and click Add.
Click Add Field to specify another field to store.
Select title from the dropdown in the Field Name column and click Add.
Click Save Changes.
The following example stores only the title and awards.wins fields in the documents in the collection. After you select your preferred configuration method, select the movies collection under the sample_mflix database.
Append lines 5 to 10 from the following example to the index definition to store the specified fields on MongoDB Search. The example uses the [dot notation](https://www.mongodb.com/docs/manual/core/document.md#std-label-document-dot-notation to specify nested fields.
{
"mappings": {
"dynamic": true,
},
"storedSource": {
"include": [
"title",
"awards.wins"
]
}
}
The following example stores all fields except directors and imdb.rating in the documents in the collection. After you select your preferred configuration method, select the movies collection under the sample_mflix database.
Click Refine Your Index to configure your index.
In the Stored Source Fields section, click All Except Specified.
Select directors from the dropdown in the Field Name column and click Add.
Click Add Field to specify another field to exclude.
Select imdb.rating from the dropdown in the Field Name column and click Add.
Click Save Changes.
The following example stores all fields except directors and imdb.rating in the documents in the collection. After you select your preferred configuration method, select the movies collection under the sample_mflix database.
Append lines 5 to 10 from the following example to the index definition to exclude the specified fields. The example uses the [dot notation](https://www.mongodb.com/docs/manual/core/document.md#std-label-document-dot-notation to specify nested fields.
{
"mappings": {
"dynamic": true,
},
"storedSource": {
"exclude": [
"directors",
"imdb.rating"
]
}
}
The following example stores all the fields in the documents in the collection. After you select your preferred configuration method, select the movies collection under the sample_mflix database.
Click Refine Your Index to configure your index.
In the Stored Source Fields section, click All.
Click Save Changes.
The following example stores all the fields in the documents in the collection. After you select your preferred configuration method, select the movies collection under the sample_mflix database.
Append line 5 from the following example to the index definition to store all of the fields.
{
"mappings": {
"dynamic": true,
},
"storedSource": true,
}
The following example for the sample_mflix.movies namespace stores only the title and awards.wins fields in the documents on mongot:
db.movies.createSearchIndex(
"default",
"definition": {
"mappings": {
"dynamic": true,
},
"storedSource": {
"include": [
"title",
"awards.wins"
]
}
}
)
The following example for the sample_mflix.movies namespace stores all fields except directors and imdb.rating in the documents.
db.movies.createSearchIndex(
"default",
"definition": {
"mappings": {
"dynamic": true,
},
"storedSource": {
"exclude": [
"directors",
"imdb.rating"
]
}
}
)
The following example for the sample_mflix.movies namespace stores all the fields in the documents in the collection.
db.movies.createSearchIndex(
"default",
"definition": {
"mappings": {
"dynamic": true,
},
"storedSource": true,
}
)
