The storedSource option in a MongoDB Search index definition specifies the fields in the source document
that MongoDB Search must store. You can configure storedSource to improve query
performance in certain use cases,
as it reduces the need for implicit query time lookup on the backend database.
You can store fields of all MongoDB Search Field Types 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 option.
Note
storedSource is only available on clusters running one of
the following versions:
MongoDB 7.0+
To learn more about retrieving the stored fields, see Return Stored Source Fields.
Syntax
The storedSource option has the following syntax in an index
definition:
1 { 2 "storedSource": true | false | { 3 "include" | "exclude": [ 4 "<field-name>", 5 ... 6 ] 7 } 8 }
Options
The storedSource option takes a boolean value or an object in the index definition.
Boolean Values
Value | Description |
|---|---|
| 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. |
| Specifies that MongoDB Search must not store the original source
document. This is the default value for the |
Object
The storedSource option object must contain one of the following fields:
Field | Type | Description |
|---|---|---|
| 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 |
| 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. |
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, the Atlas UI Visual Editor, or the Atlas UI JSON Editor to
configure the indexes.
➤ Use the Select your language drop-down menu to set the interface for the procedure on this page.
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.winsfrom the dropdown in the Field Name column and click Add.Click Add Field to specify another field to store.
Select
titlefrom the dropdown in the Field Name column and click Add.Click Save Changes.
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 to specify nested fields.
1 { 2 "mappings": { 3 "dynamic": true, 4 }, 5 "storedSource": { 6 "include": [ 7 "title", 8 "awards.wins" 9 ] 10 } 11 }
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
directorsfrom the dropdown in the Field Name column and click Add.Click Add Field to specify another field to exclude.
Select
imdb.ratingfrom the dropdown in the Field Name column and click Add.Click Save Changes.
Append lines 5 to 10 from the following example to the index definition to exclude the specified fields. The example uses the dot notation to specify nested fields.
1 { 2 "mappings": { 3 "dynamic": true, 4 }, 5 "storedSource": { 6 "exclude": [ 7 "directors", 8 "imdb.rating" 9 ] 10 } 11 }
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.
Append line 5 from the following example to the index definition to store all of the fields.
1 { 2 "mappings": { 3 "dynamic": true, 4 }, 5 "storedSource": true, 6 }
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, } )