Docs Menu
Docs Home
/ /

Text Indexes on Self-Managed Deployments

Note

This page describes text query capabilities for self-managed (non-Atlas) deployments. For data hosted on MongoDB, MongoDB also offers an improved full-text query solution, MongoDB Search and a vector search solution, Vector Search.

Text indexes support text search queries on fields that contain string content. They also support aggregation expressions for encrypted string fields in Queryable Encryption enabled collections. Text indexes improve performance when you search for specific words or strings within string content.

A collection can have only one text index, but that index may include multiple fields.

To create a text index, use the following prototype:

db.<collection>.createIndex(
{
<field1>: "text",
<field2>: "text",
...
}
)

Text indexes support $text query operations on on-premises deployments. To use $text, you must create a text index.

Text indexes support aggregation expressions for fields in Queryable Encryption collections with prefix, suffix, or substring queries enabled. A text index is required to use the $encStrNormalizedEq expression.

An online shop's clothing collection has a description field that contains a string of text describing each item. To find clothes made of "silk", create a text index on description and run a $text query for "silk". The search returns all documents mentioning "silk" in description.

To learn how to create and use text indexes, see:

This section describes text index details.

In a compound index with a text index key and other key types, only the text index field determines whether the index references a document. Other keys do not affect document references.

Text indexes can't cover a query.

Text indexes are always sparse. MongoDB ignores the sparse option when creating text indexes.

MongoDB does not add a text index entry for documents that lack the text index field, have null values, or have empty arrays.

Text indexes have these storage and performance characteristics:

  • Text indexes can consume significant RAM. They contain one index entry for each unique stemmed word in each indexed field for each document.

  • Building a text index is similar to building a large multikey index but takes longer than building an ordered (scalar) index on the same data.

  • When building large text indexes, ensure sufficient file descriptor limits. See recommended settings.

  • Text indexes impact write performance because MongoDB must add an index entry for each unique stemmed word in each indexed field of new documents.

  • Text indexes store individual words, not multi-word strings or word proximity information. Queries with multiple words run faster when the entire collection fits in RAM.

Back

Text Search Languages

On this page