Text Indexes
On this page
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", ... } )
Compatibility
You can use text indexes for deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
To learn more about managing indexes for deployments hosted in MongoDB Atlas, see Create, View, Drop, and Hide Indexes.
Text Search Support
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.
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 search 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 about text indexes, see:
For text search examples, see the
$text reference page
.For sample
$text
operations in aggregation pipelines, see Text Search in the Aggregation Pipeline.