For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

How to Index String Fields For Faceted Search

You can use the MongoDB Search stringFacet type to index string fields for faceting, which allows you to run a facet operator query on that field. MongoDB Search doesn't apply the analyzer when indexing string fields for faceting.

MongoDB Search only supports facet operator queries against fields indexed as the stringFacet type. To perform a normal search also on the same field, you must index the field as type string also.

To facet on string fields in embedded documents, you must index the parent fields as the document type. When you facet on a string field inside embedded documents, MongoDB Search returns facet count for only the number of matching parent documents.

MongoDB Search doesn't dynamically index string values for faceting. You must use static mappings to index string values for faceting. You can use the Visual Editor or the JSON Editor in the Atlas UI to index string fields as the number type.

To define the index for the stringFacet type, choose your preferred configuration method in the Atlas UI and then select the database and collection.

The MongoDB Search stringFacet type has the following parameters:

UI Field Name
JSON Option
Type
Necessity
Description
Data Type

type

string

Required

Human-readable label that identifies this field type. Value must be stringFacet. MongoDB Search truncates BSON string values indexed as the stringFacet type to around 8000 UTF-16 code units.

The following index definition example uses the sample_mflix.movies collection. If you have the sample data already loaded on your cluster, you can use the Visual Editor or JSON Editor in the Atlas UI to configure the index. After you select your preferred configuration method, select the database and collection, and refine your index to add field mappings.

The following index definition for the sample_mflix.movies collection in the sample dataset indexes the genres field as stringFacet for faceting.

The following index definition for the sample_mflix.movies collection in the sample dataset indexes the genres field as stringFacet and string types to return the following types of results for your queries:

To learn more about the facet collector and see example queries, see Examples.

To learn how to create an index with a facet definition and query that index using the facet collector, see the How to Use Facets with MongoDB Search tutorial.

To see an example index and query that uses stringFacet to run a faceted search with paginated results, see Use Facet with the Paginated Results.