Text Indexes on Self-Managed Deployments
On this page
Note
This page describes text query capabilities for self-managed (non-Atlas) deployments. For data hosted on MongoDB Atlas, MongoDB offers an improved full-text query 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 may include multiple fields.
To create a text index, use the following prototype:
db.<collection>.createIndex( { <field1>: "text", <field2>: "text", ... } )
$text Support
Text indexes support $text
query operations on on-premises
deployments. To use $text
, you must create a text index.
Use Cases
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
query for documents with the
keyword silk
. The search returns all documents that mention silk
in the description
field.
Get Started
To learn how to create text indexes and use text indexes in specific use cases, see:
Details
This section describes details for text indexes.
Compound 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.
sparse
Property
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.
Storage Requirements and Performance Costs
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.
Learn More
To learn more about text indexes, see:
For text search examples, see the
$text reference page
.For sample
$text
operations in aggregation pipelines, see $text in the Aggregation Pipeline on Self-Managed Deployments.