Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Text Indexes

On this page

  • Text Search Support
  • Use Cases
  • Get Started
  • Details
  • Compound Text Indexes
  • sparse Property
  • Storage Requirements and Performance Costs
  • Learn More

Note

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

Text indexes support text search queries on fields containing string content. Text indexes improve performance when searching for specific words or phrases within string content.

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

Indexing commonly queried fields increases the chances of covering those queries. Covered queries are queries that can be satisfied entirely using an index, without examining any documents. This optimizes query performance.

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 perform text searches, you must create a text index and use the $text query operator.

Documents in an online shop's clothing collection include a description field that contains a string of text describing each item. To find clothes made of silk, create a text index on the description field and run a text search query for documents with the keyword silk. The search returns all documents that mention silk in the description field.

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

This section describes details for text indexes.

For a compound index that includes a text index key along with keys of other types, only the text index field determines whether the index references a document. The other keys do not determine whether the index references the documents.

Text indexes are always sparse. When you create a text index, MongoDB ignores the sparse option.

If an existing or newly inserted document lacks a text index field (or the field is null or an empty array), MongoDB does not add a text index entry for the document.

Text indexes have the following storage requirements and performance costs:

  • Text indexes can take up a large amount of RAM. They contain one index entry for each unique post-stemmed word in each indexed field for each document inserted.

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

  • When building a text index that takes up a large amount of RAM, ensure that you have a sufficiently high limit on open file descriptors. See the recommended settings.

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

  • Text indexes store individual words of a text string. They do not store phrases or information about the proximity of words in the documents. As a result, queries that specify multiple words run faster when the entire collection fits in RAM.

←  Multikey Index BoundsCreate a Text Index →